nested loop for matplotlib graph of financial time series
Question: I am trying to print graphs for the selected tickers as I am learning python
and matplotlib. I have written the following code and it works fine, except
for the legend which prints the entire list of tickers, and I understand why
it is doing that, but I don't understand how to get it to print only the
ticker related to that graph. I also feel that as I am a beginner, I might
have written too many lines of code, and if someone can guide me on how to
reduce this code, that will help me learn the right way.

    
    
    #!/usr/bin/env python3
    import numpy as np
    import datetime
    import pandas as pd
    import pandas.io.data
    from pandas import Series, DataFrame
    
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    
    tickers=["ADBE","AAPL","GME","SNDK"]
    roll_pd=30
    num_std=2
    flist=[]
    ts_cutoff_days=-220
    flag=1
    start=datetime.datetime(2008,7,1)
    end=datetime.date.today()
    
    for ticker in tickers:
        time_srs=pd.io.data.get_data_yahoo(ticker,start,end)
        #time_srs.head()
        fname=ticker+"_ohlc.csv"
        print (fname)
        time_srs.to_csv(fname) 
        flist.append(fname)
    
    for fn in flist:
        #print(fn)
        df=pd.read_csv(fn,index_col='Date',parse_dates=True)
        df.head()
        df.index
        close_px=df['Adj Close']
        ts=df['Adj Close'][ts_cutoff_days:]
        if flag==0:
            px_srs=close_px
        else:
            px_srs=ts
    
        mavg=pd.rolling_mean(px_srs,roll_pd)
        mstd=pd.rolling_std(px_srs,roll_pd)
    
        rets=px_srs / px_srs.shift(1) - 1
        rets.head()
    
    
        plt.figure()
        plt.plot(px_srs.index, px_srs,color='k',label=tickers)# this is where I am making the error
        plt.plot(mavg.index, mavg,label='mavg',color='red')
        plt.fill_between(mstd.index,mavg-num_std*mstd,mavg+num_std*mstd,color='b',alpha=0.2)
        plt.legend(loc='best')
        plt.axes().yaxis.grid(True)
        plt.show()
    
    if flag==0:
        df1 = pd.io.data.get_data_yahoo(tickers,start, end)['Adj Close']
    else:
        df1 = pd.io.data.get_data_yahoo(tickers,start, end)['Adj Close'][ts_cutoff_days:]# I tried writing [ts] instead of ['Adj Close'][ts_cutoff_days:], but it gave me an error, so I had to write this above if-else condition to create df1 correctly
    


Answer: I think you should rather toss away your `flist`, as all the information is
already in `tickers`.. Then you can do:

    
    
    ...
    
    for ticker in tickers:
        df=pd.read_csv(ticker + 'ohlc.csv',index_col='Date',parse_dates=True)
        ...
        plt.plot(px_srs.index, px_srs,color='k',label=ticker)
    

If you are worried about changing the file names, then you can create a simple
function for that and use it in both reading and writing the file.

Another possibility would be to put a tuple into flist:

    
    
    for ticker in tickers:
        ...
        flist.append((ticker, fname))
    

And then use this tuple in your next loop:

    
    
    for ticker, fn in flist:
        ...
    

This also enables you to use `ticker` as the plot label.

