如何将 2x2 的 matplotlib 网格的 y 值设置为共享常量?

How can I set up the y value of a matplotlib grid of 2x2 to a shared constant?

我目前正在从事一个项目,该项目将绘制从加速度计收集的数据。为了更好地了解我们将要做什么,我需要将数据绘制成网格,这是我用 matplotlib 完成的:

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title("Non Faulty Acc X Data")
axs[0, 1].plot(x2, y2, 'tab:orange')
axs[0, 1].set_title('Non Faulty Acc X Data')
axs[1, 0].plot(x3, y3, 'tab:green')
axs[1, 0].set_title('Faulty Acc X Data')
axs[1, 1].plot(x4, y4, 'tab:red')
axs[1, 1].set_title('Faulty Acc X Data')

使用此代码,我得到以下输出。 Image of the output of the code

如您所见,两张图的 y 值(时间)不同。
我试过这段代码:

plt.ylim([0, 1])

我在每个 .set_title.plot 方法之后都单独尝试过。我也尝试在 for 循环中使用它:

for ax in axs.flat:
    plt.ylim([0, 1])
    ax.set(ylabel='Time')

之后什么都没有改变。
我基本上需要第二个图表的 y 值才能匹配第一个图表的 y 值以获得准确的输出。

Matplotlib 的 subplots() 具有 sharexsharey 参数,可自动将轴对齐到相同范围。

所以在你的情况下,你应该简单地使用 subplots(2, 2, sharey=True).

@Brendan 的回答非常有效,但更简单的方法是在 plt.subplots:

的关键字参数中指定 sharey=True
plt.subplots(2, 2, sharey=True)