2015-04-08 33 views
8

Ho una serie di dati come segue (in Python):Come creare un grafico del contorno ternario in Python?

import numpy as np 
A = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0, 0.1, 0.2, 0.3, 0.4, 0.2, 0.2, 0.05, 0.1]) 
B = np.array([0.9, 0.7, 0.5, 0.3, 0.1, 0.2, 0.1, 0.15, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) 
C = np.array([0, 0.1, 0.2, 0.3, 0.4, 0.2, 0.2, 0.05, 0.1, 0.9, 0.7, 0.5, 0.3, 0.1, 0.2, 0.1, 0.15, 0]) 
D = np.array([1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2]) 

Sto cercando di creare diagramma ternario con matplotlib come mostrato in figura (source). Gli assi sono i valori A, B, C e D dovrebbero essere indicati dai contorni e i punti devono essere etichettati come in figura.

enter image description here

Può tali grafici da istituire in matplotlib o con Python?

+0

La risposta breve sarebbe "sì", ma è un sacco di lavoro ... E non sono sicuro che stiate ottenendo queste etichette di graduazione inclinate ... – heltonbiker

risposta

4

Si può provare qualcosa di simile:

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.tri as tri 




# first load some data: format x1,x2,x3,value 
test_data = np.array([[0,0,1,0], 
         [0,1,0,0], 
         [1,0,0,0], 
         [0.25,0.25,0.5,1], 
         [0.25,0.5,0.25,1], 
         [0.5,0.25,0.25,1]]) 

# barycentric coords: (a,b,c) 
a=test_data[:,0] 
b=test_data[:,1] 
c=test_data[:,2] 

# values is stored in the last column 
v = test_data[:,-1] 

# translate the data to cartesian corrds 
x = 0.5 * (2.*b+c)/(a+b+c) 
y = 0.5*np.sqrt(3) * c/(a+b+c) 


# create a triangulation out of these points 
T = tri.Triangulation(x,y) 

# plot the contour 
plt.tricontourf(x,y,T.triangles,v) 


# create the grid 
corners = np.array([[0, 0], [1, 0], [0.5, np.sqrt(3)*0.5]]) 
triangle = tri.Triangulation(corners[:, 0], corners[:, 1]) 

# creating the grid 
refiner = tri.UniformTriRefiner(triangle) 
trimesh = refiner.refine_triangulation(subdiv=4) 

#plotting the mesh 
plt.triplot(trimesh,'k--') 


plt.show() 

Some Simple Triangular plot

Si noti che, è possibile rimuovere la x, y assi facendo:

plt.axis('off') 

Tuttavia, per l'asse triangolare + etichette e segni di spunta, non lo so ancora, ma se qualcuno ha una soluzione, la prenderò;)

migliore,

Julien

+0

Parte del codice è ispirata a [http: //blog.bogatron.net/blog/2014/02/02/visualizing-dirichlet-distributions/] –