【问题标题】:Share X axis of two pandas series' plots [duplicate]分享两个熊猫系列地块的X轴[重复]
【发布时间】:2021-11-14 11:46:38
【问题描述】:

我有两个熊猫系列。我需要用彼此堆叠的共享 X 轴来绘制它们。我尝试了下面的代码,但不是将它们堆叠起来,而是将两条线放在同一个图上。这是为什么?如何堆叠它们?

n=10
x = pd.Series(data = np.random.randint(0,10, n), index=pd.date_range(pd.to_datetime('today'), freq='D', periods=n))
y = pd.Series(data = np.random.randint(0,10, n), index=pd.date_range(pd.to_datetime('today'), freq='D', periods=n))

ax = x.plot()
ax2 = y.plot()
ax2.sharex(ax)

我知道可以使用subplots 完成堆叠。如果我理解正确的话,pandas 的唯一句柄是调用df.plot() 时返回的ax。我如何使用这个ax 来实现共享横坐标的正确堆叠?

【问题讨论】:

    标签: python pandas matplotlib plot


    【解决方案1】:

    使用subplots创建共享轴:

    fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
    x.plot(ax=ax1)
    y.plot(ax=ax2)
    plt.show()
    

    【讨论】:

    • fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True) x.plot(ax = ax1) y.plot(ax = ax2) plt.show()
    • 如何将第一个图的底部轴与底部图的顶部水平线合并?
    • 请给我一张你想要的图片好吗?
    • plt.subplots_adjust(hspace=0)?
    猜你喜欢
    • 2016-04-23
    • 2019-10-01
    • 2013-08-07
    • 2020-04-21
    • 2014-10-20
    • 2019-07-23
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    相关资源
    最近更新 更多