matplotlib 如何在python中用颜色条绘制二维三角坐标内的一维数据?

ss2ws0br  于 2022-11-15  发布在  Python
关注(0)|答案(1)|浏览(159)

我有闭合三角形数据坐标a1(尺寸10x1)和b1(10x1)、创建了3个闭合三角形。我有另一个c1数据(大小为3x1)。我想用颜色条在三角形上绘制c1数据。我试图创建的图像类似于matlab函数patch(x,y,c)。但是我无法在Python(Jupyter Notebook)中找到类似的函数,有没有人能建议如何绘制它?
以下是示例数据:

import numpy as np
import matplotlib.pyplot as plt

a1=[0.90899658,1.10720062,1.47019958,0.90899658,1.47019958,3.14479828,0.90899658,3.14479828,3.17749786,0.90899658]
b1=[-0.38689995,0.22739983,0.69180012,-0.38689995,0.69180012,-0.34249973,-0.38689995,-0.34249973,-0.38329983,-0.38689995]
plt.plot(a1,b1)

#creating triangular matrix of size 3x3 after avoiding last cordinate
a2=a1[0:9]
b2=b1[0:9]
a3=np.reshape(a2,(3,3))
b3=np.reshape(b2,(3,3))

# The data as a third function c1 for 3 triangles
c1=[0.234,0.034,0.006]

# ploting data
plt.fill(a3,b3)
plt.colorbar

输出图应如下所示(从Matlab创建):

kq0g1dla

kq0g1dla1#

我提出了下面的解决方案,如果你喜欢的话,你可以编辑颜色Map表标签。顺便说一下,我使用了下面的method来显示三角形,因为它比你的简单。

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

a1=[0.90899658,1.10720062,1.47019958,0.90899658,1.47019958,3.14479828,0.90899658,3.14479828,3.17749786,0.90899658]
b1=[-0.38689995,0.22739983,0.69180012,-0.38689995,0.69180012,-0.34249973,-0.38689995,-0.34249973,-0.38329983,-0.38689995]

# The data as a third function c1 for 3 triangles
c1=[0.234,0.034,0.006]
cmap = mpl.cm.get_cmap('Spectral')
norm = mpl.colors.Normalize(vmin=min(c1), vmax=max(c1))

# ploting data
fig, ax = plt.subplots()
for i in range(0, 9, 3):
    atest = np.append(a1[i:i+3], a1[i])
    btest = np.append(b1[i:i+3], b1[i])
    ax.fill(atest,btest,color=cmap(norm(c1[(int)(i/3)])))
    
cbar = plt.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap))
plt.show()

输出:

相关问题