2013-11-01 36 views
3

我有一個包含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()) 

它不會改變在網格中的數字,但不是實際的數據。這不是解決方案。任何人都可以在這裏給我一個幫助?

回答

7

範圍只是將這些角座標分配給數據,它並不會改變底層數據的順序。

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.flipudnp.fliplr鏡像陣列的軸線。但是我個人更喜歡用imshow來設置原點,如果足夠的話。

+0

在imshow中添加origin ='lower'完美的作品。謝謝! – Rotail