python - 如何在 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()
解决方案
推荐阅读
- r - 比较向量的名称并选择常见的并将值相加
- javascript - 通过递归检查每个字符串编号
- java - Java 正则表达式查找或替换第一次出现而不丢失尾部文本
- sql - 我想通过 cdValuse 选择相同的 Table 相同的列来分隔 Desc_Main 和 Desc_sup
- mysql - 用于计数和显示(列中的不同值)的 Sql 查询优化,按其他两列分组
- python - 如何从字符串中找到多字字符串,并在python中标记它?
- java - BottomAppBar 抛出 IllegalArgumentException
- javascript - Node.js 相当于:echo 'message' | 数控-v
- c - C:scanf 没有在无限循环中停止
- javascript - 单击按钮后,生成一个字符串数组,将其显示在页面上,一个按钮将生成另一个随机数组