【问题标题】:Changing the origin of a plot更改绘图的原点
【发布时间】:2013-11-01 14:10:24
【问题描述】:

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

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 plot axis


    【解决方案1】:

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

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

    您还可以考虑使用np.flipudnp.fliplr 来镜像阵列的轴。但如果足够的话,我个人更喜欢用 imshow 设置原点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      相关资源
      最近更新 更多