【问题标题】:How to plot multiple lines in subplot using python and matplotlib如何使用 python 和 matplotlib 在子图中绘制多条线
【发布时间】:2021-12-29 20:14:46
【问题描述】:

我一直在关注Merge matplotlib subplots with shared x-axis 提供的解决方案。请参阅解决方案 35。在每个子图中,有一条线,但我想在每个子图中有多条线。例如,上面的图有 IBM 的价格和 30 天移动平均线。底部图有 180 天和 30 天的方差。

为了在我的其他 python 程序中绘制多行,我使用了(data).plot(figsize=(10, 7)),其中data 是按日期索引的数据框,但在作者的解决方案中,他使用line0, = ax0.plot(x, y, color='r') 将数据系列 (x,y) 分配给阴谋。在解决方案 35 中存在多行的情况下,如何将具有多列的数据框分配给绘图?

【问题讨论】:

    标签: python matplotlib subplot


    【解决方案1】:

    您需要使用 (data).plot(ax=ax0) 来处理 pandas 绘图。 对于您可以使用的图例:

    handles0, labels0 = ax0.get_legend_handles_labels()
    handles1, labels1 = ax1.get_legend_handles_labels()
    ax0.legend(handles=handles0 + handles1, labels=labels0 + labels1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-17
      • 2014-07-27
      • 2017-01-02
      • 1970-01-01
      • 2018-05-18
      • 2022-01-19
      • 2019-01-31
      • 1970-01-01
      相关资源
      最近更新 更多