2013-08-28 44 views
1

我想知道如果有一個更優雅的方式(通過消除任何for循環,或通過其他方式來繪製網格),達到這個數字:Matplotlib:雙軸和set_equal

grid

nx, ny = 8, 6 
fig = plt.figure() 

ax = fig.add_subplot(111) 
ax.set_xlabel('Pixel X-coordinate') 
ax.set_ylabel('Pixel Y-coordinate') 
for i in range(nx-1): 
    ax.axvline(i+0.5, color='k') 
for i in range(ny-1): 
    ax.axhline(i+0.5, color='k') 

ax_twiny = ax.twiny() 
ax_twiny.set_xticks(np.arange(nx-1) + 0.5) 
ax_twiny.set_xticklabels(np.linspace(-600, 600, nx-1)) 
ax_twiny.set_xlabel(u'World X-coordinate [µm]') 

ax_twinx = ax.twinx() 
ax_twinx.set_yticks(np.arange(ny-1) + 0.5) 
ax_twinx.set_yticklabels(np.linspace(-400, 400, ny-1)) 
ax_twinx.set_ylabel(u'World Y-coordinate [µm]') 

for a in (ax, ax_twiny, ax_twinx): 
    a.set_xlim(-0.5, nx - 0.5) 
    a.set_ylim(-0.5, ny - 0.5) 
    a.set_aspect('equal') 

我試圖設置次刻度繪製使用ax.[xy]axis.grid(True, which='minor')網格,但是當我繪製雙軸(使用matplotlib1.1)網格消失。

+0

http://sourceforge.net/mailarchive/forum.php?thread_name=012701ce5892%24683e5cb0%2438bb1610 %24%40earthlink.net&forum_name = matplotlib-devel < - 可能是有趣的 – tacaswell

回答

1

不知道,還有一個更優雅的方式,不過地圖可以刪除最後一個循環:

def setStuff(ax): 
    ax.set_xlim(-0.5, nx - 0.5) 
    ax.set_ylim(-0.5, ny - 0.5) 
    ax.set_aspect('equal') 
map(setStuff, [ax, ax2, ax3])