繁体   English   中英

如何有效地使用 plot 这个 matplotlib 的代码

[英]How to plot this code of matplotlib efficiently

我是 python 的新手,并对股票进行时间序列分析。我根据收盘价的百分比变化创建了 5 只股票的滚动平均值数据框。因此,这个 df 有 5 列,我还有另一个 df 指数滚动平均值百分比收盘价的变化。我想用索引df的df的plot个股列。 我写了这段代码

fig.add_subplot(5,1,1)
plt.plot(pctchange_RA['HUL'])
plt.plot(N50_RA)    

fig.add_subplot(5,1,2)
plt.plot(pctchange_RA['IRCON'])
plt.plot(N50_RA)    

fig.add_subplot(5,1,3)
plt.plot(pctchange_RA['JUBLFOOD'])
plt.plot(N50_RA)    

fig.add_subplot(5,1,4)
plt.plot(pctchange_RA['PVR'])
plt.plot(N50_RA)    

fig.add_subplot(5,1,5)
plt.plot(pctchange_RA['VOLTAS'])
plt.plot(N50_RA)   


NOTE:pctchange_RA is a pandas df of 5 stocks and N50_RA is a index df of one column

您可以将列名放在列表中,然后循环遍历它并动态创建子图。 伪代码如下所示

cols = ['HUL', 'IRCON', 'JUBLFOOD', 'PVR', 'VOLTAS']

for i, col in enumerate(cols):
    ax = fig.add_subplot(5, 1, i+1)
    ax.plot(pctchange_RA[col])
    ax.plot(N50_RA)   

暂无
暂无

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

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