python - 奇怪的行为 : pcolormesh and meshgrid (easy)

标签 python matplotlib plot

我有一些数据 phi,我想用 pcolormesh 绘制它。 我的第一个方法是:

plt.figure()
plt.colormesh(phi)
plt.show()

产生:

Without Cut

好。现在我只想稍微调整一下轴,因此我 使用网格:

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()

现在我得到以下信息:

with cut

正如你可以清楚地看到的,顶部的条纹被切断了!我无法解释这种行为,因为我对 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/

相关文章:

python - PyCharm 导入 MySQL

python - 过滤成对(元组)列表,其中元组不包含来自另一个列表的任何值

python - 如何在wxPython中使用鼠标旋转matplotlib 3D绘图?

plot - Vega-Lite:是否可以绘制一个 3 层图,其中一个 Y 轴仅由 2 个特定层使用?

python - 如何删除用于 Pylab 绘图的日志文件中的第一行?

r - 3d 模型图(3d 散点图 + 模型表面 + 连接点到表面)

python - 使用文件作为子进程的标准输入和标准输出

python - 属性错误 : Can't pickle local object '<locals>.<lambda>'

python - pylab 找不到其模块的引用

python - 设置 matplotlib 子图的绝对大小