matplotlib 与另一个图中的 y 轴共享 x 轴

s_h*_*key 12 python matplotlib

如果我想将两个独立轴的 x 轴和 y 轴连接在一起,以便它们一起缩放,我通常会这样做:

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122,sharex=ax1, sharey=ax1)
Run Code Online (Sandbox Code Playgroud)

但我不知道如何将一个图的 x 轴与另一个图的 y 轴共享。例如,一个图的 x 轴是“时间”,我想与另一个也代表“时间”的图的 y 轴共享它。像这样的东西(这不起作用......):

ax2 = fig.add_subplot(122,sharex=ax1.yaxis, sharey=ax1.xaxis)
Run Code Online (Sandbox Code Playgroud)

小智 0

我会做这样的事情,

fig, axs = plt.subplots(3, sharex=True, sharey=True) //Here
fig.suptitle('Sharing both axes')
axs[0].plot(x, y ** 2)
axs[1].plot(x, 0.3 * y, 'o')
axs[2].plot(x, y, '+')

Run Code Online (Sandbox Code Playgroud)

根据 matplotlib 文档,对我来说也很好用

另一种方法是您也可以叠加。