【问题标题】:How to plot this code of matplotlib efficiently如何有效地绘制 matplotlib 的这段代码
【发布时间】:2020-08-21 05:39:33
【问题描述】:

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

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

【问题讨论】:

    标签: python-3.x pandas matplotlib time-series


    【解决方案1】:

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

    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)   
    

    【讨论】:

      猜你喜欢
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 2012-11-04
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多