簡體   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