matplotlib上的三元图:如何添加带有轴名称和颜色图例的三角轴?

时间:2018-10-26 08:03:37

标签: python matplotlib ternary

问题在标题中。我正在运行脚本(在下面查找):

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import matplotlib.cm as cm
import cantera as ct

gas = ct.Solution('gri30.cti')
comp_air = "O2:1, N2:3.76"

lambd = 1
npts = 100
Tmel = 1500
p = 101325

propCH4 = [100,90,90,80,80,80,70,70,70,70,60,60,60,60,60,50,50,50,50,50,50,40,40,40,40,40,40,40,30,30,30,30,30,30,30,30,20,20,20,20,20,20,20,20,20,10,10,10,10,10,10,10,10,10,10,0,0,0,0,0,0,0,0,0,0,0]
propH2 = [0,10,0,20,10,0,30,20,10,0,40,30,20,10,0,50,40,30,20,10,0,60,50,40,30,20,10,0,70,60,50,40,30,20,10,0,80,70,60,50,40,30,20,10,0,90,80,70,60,50,40,30,20,10,0,100,90,80,70,60,50,40,30,20,10,0]
propN2 = [0,0,10,0,10,20,0,10,20,30,0,10,20,30,40,0,10,20,30,40,50,0,10,20,30,40,50,60,0,10,20,30,40,50,60,70,0,10,20,30,40,50,60,70,80,0,10,20,30,40,50,60,70,80,90,0,10,20,30,40,50,60,70,80,90,100]
NO = np.zeros(len(propCH4))
comp_gas = np.zeros(len(propCH4))


for i in range (len(propCH4)):   
    comp_gas ="CH4:"+str(propCH4[i])+" , H2:"+str(propH2[i])+" , N2:"+str(propN2[i])+""
    gas.set_equivalence_ratio(1/lambd, comp_gas, comp_air) 
    gas.TPX = Tmel, p, gas.mole_fraction_dict()
    gas.equilibrate('HP')
    mole_fraction= list(gas.mole_fraction_dict().values())
    key= list(gas.mole_fraction_dict().keys())
    for j in range(len(key)):
        if key[j] == 'NO':
            NO[i] = mole_fraction[j]*10000

data = np.zeros((len(propCH4)+3,4))
data[0] = [0,0,1,0]
data[1] = [0,1,0,0]
data[2] = [1,0,0,0]
for i in range(3,len(propCH4)+3,1):
    data[i] = [propCH4[i-3], propH2[i-3], propN2[i-3], NO[i-3]]



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

# values is stored in the last column
v = 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,cmap=cm.get_cmap(name='jet', lut=None))
#'plasma'
#'jet'
#'inferno'
#'magma'


# 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=3)

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



plt.axis('off')
plt.title("NO ppm")
plt.legend
plt.savefig("NO ternary.png",dpi=600,bbox_inches='tight')
print("\nT = "+str(Tmel)+" K\nP = "+str(p)+" Pa\nλ = "+str(lambd)+"")
plt.show()

Ternary diagram

我正在使用cantera(对于那些不知道它是什么的人,它是计算平衡化学的模块)。

为了使其更具可读性,我想添加轴名称和图例,如下图所示:

Ternary diagram with legend and Axis name

有人知道怎么做吗?

感谢您的时间和对我的帮助,

致谢

1 个答案:

答案 0 :(得分:0)

更新

我发现了如何使用下面的这两行来添加图例:

cbar = plt.colorbar()
cbar.set_label('NO concentration ppm',labelpad=10)

但是仍然不知道如何在三角形上添加轴...

更多,我想为每个图形保持图例的值相同,因为例如当我改变温度时,颜色分布不会改变,而图例是。

谢谢