我有一些数据 phi,我想用 pcolormesh 绘制它。 我的第一个方法是:
plt.figure()
plt.colormesh(phi)
plt.show()
产生:
好。现在我只想稍微调整一下轴,因此我 使用网格:
plt.figure()
kk = np.arange(0,phi.shape[1])
gg = np.arange(-phi.shaoe[0]//2 +1 , phi.shape[0]//2 +1,1)
GG, KK = np.meshgrid(gg,kk)
plt.pcolormesh(KK,GG,phi.T)
plt.show()
现在我得到以下信息:
正如你可以清楚地看到的,顶部的条纹被切断了!我无法解释这种行为,因为我对 phi 函数没有任何更改!我刚刚用网格添加了轴数据!这是什么错误,如何解决!
PS:如果你想重现这个 bug,只需使用一个与 y 轴平行的轴对称的矩阵即可。
最佳答案
pcolor
文档:http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.pcolor
Ideally the dimensions of X and Y should be one greater than those of C; if the dimensions are the same, then the last row and column of C will be ignored.
因此您需要创建两个数组,其中多一行和多一列:
import pylab as pl
import numpy as np
phi = np.zeros((20, 30))
phi[0, :] = 1
phi[-1, :] = 2
kk = np.arange(0,phi.shape[1]+1)
gg = np.arange(-phi.shape[0]//2 +1 , phi.shape[0]//2 + 2)
GG, KK = np.meshgrid(gg,kk)
pl.pcolormesh(KK,GG,phi.T)
关于python - 奇怪的行为 : pcolormesh and meshgrid (easy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24494879/