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

如何使用matplotlib为python中的子地块设置相同的比例,matplotlib,Matplotlib,我想轻松直观地比较子地块。为此,我想为所有子地块设置相同的比例 我的代码运行良好,我能够绘制子图,但具有自己的比例。我希望保持x轴上的比例。如果希望有两个子地块具有相同的x轴,则可以在创建第二个轴时使用: 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)

我想轻松直观地比较子地块。为此,我想为所有子地块设置相同的比例


我的代码运行良好,我能够绘制子图,但具有自己的比例。我希望保持x轴上的比例。

如果希望有两个子地块具有相同的x轴,则可以在创建第二个轴时使用:

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()
结果:

如果要使用子批次:

fig,axs = plt.subplots(2,1, figsize = (10,8), sharex=True)

x = np.random.randn(1000)
x1 = x + 3
sns.histplot(x, ax = axs[0])
sns.histplot(x1, ax = axs[1])
fig.show()

谢谢您的回复。