简体   繁体   中英

How to plot this code of matplotlib efficiently

I am new to python and doing a time series analysis of stocks.I created a data frame of rolling average of 5 stocks according to their percentage change in close price.Therefore this df has 5 columns and i have another df index rolling average of percentage change of closing price.I want to plot individual stock column of the df with the index df. I wrote this code

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

You can put your column names in a list and then just loop over it and create subplots dynamically. A pseudocode would look like the following

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)   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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