如何在for循环中共享x和sharey轴

2024-10-03 13:26:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着分享我的sumplots的x轴和y轴,我尝试了几种不同的方法来使用sharey和sharex,但是没有得到正确的结果。在

ax0 = plt.subplot(4,1,1)
for i in range(4):
    plt.subplot(4,1,i+1,sharex = ax0)
    plt.plot(wavelength[i],flux)
    plt.xlim([-1000,1000])
    plt.ylim([0,1.5])
    plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

Tags: 方法inforplotrangepltfluxylim
1条回答
网友
1楼 · 发布于 2024-10-03 13:26:27

如果你能正确地理解x轴和y轴。这可以用plt.subplots和关键字sharex=True和{}来实现。参见以下示例:

import numpy as np
import matplotlib.pyplot as plt

fig, axlist = plt.subplots(4, 1, sharex=True, sharey=True)
for ax in axlist:
    ax.plot(np.random.random(100))

plt.show()

相关问题 更多 >