[英]Matplotlib contour plot using specific data points from a set
I am very sorry for the cumbersome title, but I don't find a way to express it clearly.对繁琐的标题感到非常抱歉,但我没有找到表达清楚的方法。 The task that I should accomplish here is, given three numpy arrays containing coordinate matrices (X, Y) and the evaluation of a real function in this grid (Z);我在这里应该完成的任务是,给定三个包含坐标矩阵 (X, Y) 的 numpy 数组以及对该网格 (Z) 中实函数的评估; to obtain contour plots of the data.以获得数据的等高线图。 However, some of the coordinates give place to unacceptable Z values, and shouldn't be considered in the plots.然而,一些坐标让位给不可接受的 Z 值,不应在图中考虑。 What I have done so far:到目前为止我做了什么:
cmap = plt.cm.get_cmap("winter")
cmap.set_under("magenta")
cmap.set_over("yellow")
with PdfPages('myplot.pdf') as pdf:
fig = plt.figure()
CS = plt.contourf(X, Y, Z, cmap=cmap)
cbar = plt.colorbar(CS)
cbar.ax.set_ylabel('Z')
plt.xlabel('X (\AA)')
plt.ylabel('Y (\AA)')
plt.tight_layout()
pdf.savefig(fig)
However, I don't find a proper way to restrict the values that should be taken into account in the plots (something like Zmin < Z < Zmax
).但是,我没有找到一种正确的方法来限制应该在图中考虑的值(例如Zmin < Z < Zmax
)。 I thought on the methods set_under
and set_over
of cmap
, but it doesn't seem to be the way.我想到了cmap
set_under
和set_over
方法,但似乎不是这样。 Any suggestion abut this problem?关于这个问题有什么建议吗? Thank you very much in advance.非常感谢您提前。
There are several ways to achieve the desired effect:有几种方法可以达到预期的效果:
1) cap your Z-values directly as below, then plot the resulting array: 1) 直接按如下所示设置 Z 值,然后绘制结果数组:
Z2 = Z.copy()
Z2[Z<Zmin] = Zmin
Z2[Z>Zmax] = Zmax
CS = plt.contourf(X, Y, Z2, cmap=cmap)
2) rescale your colorbar using the vmin
and vmax
arguments: 2) 使用vmin
和vmax
参数重新调整vmin
:
CS = plt.contourf(X, Y, Z, cmap=cmap, vmin=Zmin, vmax=Zmax)
Misread your question / intent.误读了您的问题/意图。 If you want to mark the out-of-range values, then either set them to NaNs (in which case the corresponding locations will be white), or use your set_under
/ set_over
approach together with the vmin
and vmax
arguments.如果您想标记超出范围的值,请将它们设置为 NaN(在这种情况下,相应的位置将为白色),或者将您的set_under
/ set_over
方法与vmin
和vmax
参数一起使用。
1) set out-of-range values to NaN: 1) 将超出范围的值设置为 NaN:
Z2 = Z.copy()
Z2[Z<Zmin] = np.nan
Z2[Z>Zmax] = np.nan
CS = plt.contourf(X, Y, Z2, cmap=cmap)
2) set_under
and set_over
, then set limits using the vmin
and vmax
arguments: 2) set_under
和set_over
,然后使用vmin
和vmax
参数设置限制:
cmap.set_under("magenta")
cmap.set_over("yellow")
CS = plt.contourf(X, Y, Z, cmap=cmap, vmin=Zmin, vmax=Zmax)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.