gpt4 book ai didi

python - 如何在 matplotlib 中自定义阴影范围?

转载 作者:行者123 更新时间:2023-12-01 03:29:39 24 4
gpt4 key购买 nike

问题

我有两个 100x100 数组 - 一个包含数据(我将其称为 array1 ),另一个包含一些较小的缩放数据(我将其称为 array2 )。我创建了一个测试数组来查看array2在哪里+ array1高于或低于特定阈值,每个点的结果应该是三个结果之一-array1 + array2该点 >5、>10 或两者都不是 (<5)。根据结果​​,我创建了一个新的 100x100 数组 (array3),并分别为该点分配了 1.、2. 或 0.。

现在我想使用 plt.contourf() 绘制 array1 和 array3 ,我希望后面的子图有孵化。我希望该阴影线的范围为 x=0、x=1 和 x=2。查看文档,我知道我可能无法这样做,所以我会说 x<1、1<=x、<2 和 x=>2。唯一的问题是我不知道如何指定这些范围。

代码(工作示例)

这是问题的完整代码。

#Make data array1
array1 = np.empty([10,10])
for i in range(10):
for j in range(10):
array1[i,j] = i+j
print array1

#Make data array2
array2 = np.empty([10,10])
for i in range(10):
for j in range(10):
array2[i,j] = (i*0.25)+(j*0.25)
print array2

#Make range test array3
array3 = np.empty([10,10])
for i in range(10):
for j in range(10):
if array1[i,j]+array2[i,j] > 5 and array1[i,j]+array2[i,j] > 10:
array3[i,j] = 2
elif array1[i,j]+array2[i,j] > 5:
array3[i,j] = 1
else:
array3[i,j] = 0
print array3

#Plot
from matplotlib.patches import Ellipse, Polygon
xgrid = np.arange(0,10)
ygrid = np.arange(0,10)
n_levels=2
plt.contourf(xgrid, ygrid, array1)
stip = plt.contourf(xgrid, ygrid, array3, n_levels, colors='none',
hatches=[None,'.', '/'],
extend='lower')
#create a legend for the contour set
artists, labels = stip.legend_elements()
plt.legend(artists, labels, handleheight=2)
plt.show

enter image description here

注意图例。我希望范围为 x<1、1<=x、<2 和 x=>2,而不是包自动分配的范围。

问题

如何为我的孵化分配自定义范围?我计划让这个示例正常工作,这样我就可以将其转换为 basemap ,其中有经纬度数据,最上面有一个点画数组,其中经纬度数据与数据+标准差进行比较。另外两个数组。我想要各种点画,具体取决于每个网格点的经纬度数据是否 > 一个数据+S.D。数组,其他数据+S.D.数组或两个数组。

最佳答案

要对轮廓级别进行更多控制,请使用 levels 关键字。

stip = plt.contourf(xgrid, ygrid, array3, levels=[-1, 0, 1, 2],
colors='none', hatches=[None,'.', '/'])
artists, labels = stip.legend_elements()
plt.legend(artists, labels, handleheight=2)

enter image description here

关于python - 如何在 matplotlib 中自定义阴影范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41069887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com