更改imshow原点

问题描述 投票:0回答:1

我有一个包含 3 列的数据表,我想根据前两列在彩色二维图中绘制第三列。例如下表,即

4.0 4.0 0.313660827978 
4.0 5.0 0.365348418405 
4.0 6.0 0.423733120134 
5.0 4.0 0.365348418405 
5.0 5.0 0.439599930621 
5.0 6.0 0.525083754405 
6.0 4.0 0.423733120134 
6.0 5.0 0.525083754405 
6.0 6.0 0.651536351379

我使用以下代码:

x,y,z = np.loadtxt('output_overlap.dat').T #Transposed for easier unpacking
nrows, ncols = final_step_j-1, final_step_k-1
grid = z.reshape((nrows, ncols))
plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min()),
           interpolation='nearest', 
           cmap='binary')
fig1 = plt.gcf()
plt.colorbar()
plt.xlabel('m1')
plt.ylabel('m2')
plt.draw()
fig1.savefig('test.pdf', dpi=100)
close('all')

这给了我以下情节: https://dl.dropboxusercontent.com/u/31460244/test.png

哪个是正确的。现在,我的问题是:如何更改 Y 轴上显示数据的顺序?我希望点 (4,4) 位于原点。

我尝试过改变

plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min())

至:

plt.imshow(grid, extent=(x.min(), x.max(), y.min(), y.max())

它确实改变了网格中的数字,但没有改变实际数据。这不是解决方案。有人可以帮我吗?

python matplotlib imshow yaxis
1个回答
12
投票

范围只是将这些角坐标分配给数据,无论如何它都不会改变底层数据的顺序。

imshow
有一个
origin
关键字,请参阅:

a = np.array([0.313660827978, 0.365348418405, 0.423733120134, 
              0.365348418405, 0.439599930621, 0.525083754405, 
              0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)

extent = [4,6,4,6]

fig, axs = plt.subplots(1,2)

axs[0].imshow(a, extent=extent, interpolation='none')
axs[1].imshow(a, origin='lower', extent=extent, interpolation='none')

enter image description here

您还可以考虑使用

np.flipud
np.fliplr
来镜像阵列的轴。但我个人更喜欢用 imshow 设置原点,如果这样就足够了。

© www.soinside.com 2019 - 2024. All rights reserved.