How to create ternary contour plot in Python?

后端 未结 4 2061
余生分开走
余生分开走 2020-12-13 02:53

I have a data set as follows (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         


        
4条回答
  •  自闭症患者
    2020-12-13 03:22

    You can try something like that:

    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

    Note that, you can remove the x,y axes by doing:

    plt.axis('off')
    

    However, for the triangular axis + labels and ticks, I don't know yet, but if anyone has a solution, I'll take it ;)

    Best,

    Julien

提交回复
热议问题