在 Matplotlib 中共享轴时显示刻度标签

matplotlibpythondata visualization更新于 2025/5/31 23:08:17

要在共享轴时显示刻度标签,我们只需使用带有 sharey 参数的 subplot() 方法即可。默认情况下,y 个刻度标签可见。

步骤

  • 设置图形大小并调整子图之间和周围的填充。

  • 使用 subplot() 方法向当前图形添加子图,其中 nrows=1、ncols=2 和 index=1 对应于轴 ax1。

  • 在轴 1 上绘制一条线。

  • 使用 subplot() 方法向当前图形添加子图,其中 nrows=1、ncols=2 和 index=2 对应于轴 ax2。

  • 在轴上绘制一条线2.

  • 要显示图形,请使用 show() 方法。

示例

from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
ax1 = plt.subplot(1, 2, 1)
ax1.plot([1, 4, 9])
ax2 = plt.subplot(1, 2, 2, sharey=ax1)
ax2.plot([1, 8, 27])
plt.show()

输出


相关文章


有用资源