2015-04-03 106 views
1

我想設置的座標從1到N,而不是0到NMatplotlib設定座標

我不得不嘗試使用set_ylim()set_ybound,但未能成功。

# Plot the pic. 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.set_title("Distribution of sequence order correlation values") 
ax.axes.set_xlabel("Column index") 
ax.axes.set_ylabel("Row index") 
cax = ax.imshow(tar_data, interpolation='nearest') 
cbar = fig.colorbar(cax) 

enter image description here

+0

你試過'plt.gca()。invert_yaxis()'? – ThePredator 2015-04-03 07:53:21

+0

或者因爲這是'imshow',請嘗試'origin ='lower''或'origin ='upper'' – ThePredator 2015-04-03 07:54:18

回答

2

這裏是一個解決方案。它有兩個摺疊。

首先,您可以使用imshow函數的extent關鍵字指定軸的範圍。如果你想讓第一個像素的中心位於第一個位置,這意味着像素的開始位置在第0.5位。同樣,如果最後一個像素的中心位於第8個位置,則像素的末端位於8.5。這就是爲什麼你在我的代碼中看到範圍從0.5到nx+0.5,其中nx是x方向上的點數。

完成此操作後,您的座標軸將從0.5到8.5。所以,你的蜱蟲會。這不是很漂亮。要改變這種情況,您可以使用ax.set_xticks()ax.set_yticks()重新定義您的滴答從1到8。

import numpy as np 
import matplotlib.pyplot as plt 

data = np.array([[1,23,12],[24,12,7],[14,9,4] ]) 
ny, nx = data.shape 

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.imshow(data, interpolation='nearest', extent=[0.5, nx+0.5, ny+0.5, 0.5]) 

xticks = np.arange(nx)+1 
yticks = np.arange(ny)+1 

ax.set_xticks(xticks) 
ax.set_yticks(yticks) 

plt.show() 

Result of this code