如何使用matplotlib为python中的子图设置相同的比例

mau*_*hta 6 matplotlib

我想轻松地比较子图.为此,我想为所有子图设置相同的比例.

我的代码工作正常,我能够绘制子图,但有自己的尺度.我想在x轴上保持比例.

Max*_*Noe 12

如果要使用相同x 轴的两个子图,则可以在创建第二个轴时使用sharex-keyword:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2, sharex=ax1)


t = np.linspace(0, 1, 1000)

ax1.plot(t, np.sin(2 * np.pi * t))
ax2.plot(t, np.cos(2 * np.pi * t))

plt.show()
Run Code Online (Sandbox Code Playgroud)

结果: 结果