首页 > 解决方案 > 如何在 matplotlib 中修复子图大小

问题描述

我在用 matplotlib 绘制数据时遇到了一些麻烦。我想要的是在六个图中绘制多条曲线,这些曲线排列为 2 行和 3 列。一切都很好,但是当 x 轴上的最大值发生变化时,刻度的位置会发生变化,这会导致每个子图的变化。有没有办法固定每个子图的大小?我附上了我的部分代码和屏幕截图以显示实际发生的情况。

fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(nrows=2, ncols=3)

ax1.plot(x1, y1)
ax1.set_xlim(0, max(x1))
ax1.set_ylim(-0.5, 1.05)
ax1.xaxis.set_major_locator(plt.MaxNLocator(5))

ax2.plot(x2, y2)
ax2.set_xlim(0, max(x2))
ax2.set_ylim(-0.5, 1.05)
ax2.xaxis.set_major_locator(plt.MaxNLocator(5))

ax3.plot(x3, y3)
ax3.set_xlim(0, max(x3))
ax3.set_ylim(-0.5, 1.05)
ax3.xaxis.set_major_locator(plt.MaxNLocator(5))

ax4.plot(x4, y4)
ax4.set_xlim(0, max(x4))
ax4.set_ylim(-0.5, 1.05)
ax4.xaxis.set_major_locator(plt.MaxNLocator(5))

ax5.plot(x5, y5)
ax5.set_xlim(0, max(x5))
ax5.set_ylim(-0.5, 1.05)
ax5.xaxis.set_major_locator(plt.MaxNLocator(5))

ax6.plot(x6, y6)
ax6.set_xlim(0, max(x6))
ax6.set_ylim(-0.5, 1.05)
ax6.xaxis.set_major_locator(plt.MaxNLocator(5))
plt.tight_layout()
plt.show()

在此处输入图像描述

标签: pythonmatplotlib

解决方案


推荐阅读