Matplotlib:双轴和set_equal

时间:2013-08-28 23:54:54

标签: matplotlib multiple-axes

我想知道是否有更优雅的方式(通过删除任何for循环,或使用其他方式绘制网格),来实现这个数字:

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')绘制网格,但是当我绘制双轴时(使用matplotlib 1.1),网格消失。

1 个答案:

答案 0 :(得分: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] )