我想知道是否有更优雅的方式(通过删除任何 for 循环,或通过使用另一种方式来绘制网格)来实现这个数字:
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
)时网格消失了。