【发布时间】:2020-09-21 05:10:26
【问题描述】:
我有一些二维数据,x 和 y 坐标都在 [0,1] 内,使用 pcolormesh 绘制。 现在我想将 x 和 y 坐标的图对称化为 [-0.5, 0.5]。在 Matlab 中,我可以通过改变 x 和 y 来实现这一点,例如[0, 0.2, 0.4, 0.6, 0.8] 到 [0, 0.2, 0.4, -0.4, -0.2],无需重新排列数据。但是,使用 pcolormesh 我无法得到想要的结果。
下面是一个最小的例子,数据简单地用x+y表示:
import matplotlib.pyplot as plt
import numpy as np
x,y = np.mgrid[0:1:5j,0:1:5j]
fig,(ax1,ax2,ax3) = plt.subplots(1,3,figsize=(9,3.3),constrained_layout=1)
# original plot spanning [0,1]
img1 = ax1.pcolormesh(x,y,x+y,shading='auto')
# shift x and y from [0,1] to [-0.5,0.5]
x = x*(x<0.5)+(x-1)*(x>0.5)
y = y*(y<0.5)+(y-1)*(y>0.5)
img2 = ax2.pcolormesh(x,y,x+y,shading='auto') # similar code works in Matlab
# for this specific case, the following is close to the desired result, I can just rename x and y tick labels
# to [-0.5,0.5], but in general data is not simply x+y
img3 = ax3.pcolormesh(x+y,shading='auto')
fig.colorbar(img1,ax=[ax1,ax2,ax3],orientation='horizontal')
【问题讨论】:
-
你必须重新排列数据,即使在 Matlab 中也是如此。
-
@JodyKlymak 我只是仔细检查了在 Matlab 中我正在做相同问题的 3D 版本并使用了等值面,它有效。在 Matlab 中使用 pcolor 确实是不够的。我天真地想通过移动 X 和 Y 坐标来有效地重新排列“Z”数据,但也许这只适用于散点图......
标签: python-3.x numpy matplotlib