繁体   English   中英

如何使用 python 和 matplotlib 在子图中 plot 多行

[英]How to plot multiple lines in subplot using python and matplotlib

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

To plot multiple lines in my other python programs I used (data).plot(figsize=(10, 7)) where data is a dataframe indexed by date, but in the author's solution he uses line0, = ax0.plot(x, y, color='r')将数据系列 (x,y) 分配给 plot。 在解决方案 35 中多行的情况下,如何将具有多列的 dataframe 分配给 plot?

您需要使用(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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM