Matplotlib 与 plyplot.subplot() 命令共享轴?

Matplotlib share axes with plyplot.subplot() command?

我一直在想这个问题:当我使用 plt.subplot(221)(例如)命令时,如何在 Matplotlib 中设置图形以共享坐标轴?而不是说:

fig, axes = plt.suplots(2,2,sharex=True, sharey=True)

我想说:

plt.subplot(221, sharex=True, sharey=True)

这样我就不必处理 plt.subplots returns 这个烦人的数组,但显然这行不通。

您可以使用元组解包来避免创建 axes 数组:

fig, ((ax1, ax2), (ax3, ax4)) = plt.suplots(2, 2, sharex=True, sharey=True)

之后就可以正常绘图了。

例如ax2.plot(...) 然后将绘制到右上角的子图。