[Python] Plotting multiple stocks

Source: Internet
Author: User
Tags stock prices

ImportOSImportPandas as PDImportMatplotlib.pyplot as PltdefTest_run (): start_date='2017-01-01'End_data='2017-12-15'dates=Pd.date_range (start_date, End_data)#Create an empty data frameDF=PD. DataFrame (index=dates) Symbols=['SPY','AAPL','IBM','GOOG','GLD']     forSymbolinchsymbols:temp=getadjcloseforsymbol (symbol) DF=df.join (temp, how='Inner')    returnDFdefGetadjcloseforsymbol (symbol):#Load csv fileTemp=pd.read_csv ("Data/{0}.csv". Format (symbol), Index_col="Date", Parse_dates=True, Usecols=['Date','ADJ Close'], na_values=['nan'])    #Rename the columnTemp=temp.rename (columns={'ADJ Close': Symbol}) returnTempdefPlot_data (DF, title="Stock Prices"): Ax=df.plot (Title=title, fontsize=10) Ax.set_xlabel ("Date") Ax.set_ylabel (" Price") plt.show ()if __name__=='__main__': DF=Test_run ()#data=data.ix[' 2017-12-01 ': ' 2017-12-15 ', [' IBM ', ' GOOG ']plot_data (DF)"""IBM GOOG 2017-12-01 154.759995 1010.169983 2017-12-04 156.460007 998.679993 2017-12-05 155.350006 1005.150024 2017-12-06 154.100006 1018.380005 2017-12-07 153.570007 1030.930054 20 17-12-08 154.809998 1037.050049 2017-12-11 155.410004 1041.099976 2017-12-12 156.740005 1040.479980 2017-12 -13 153.910004 1040.609985 2017-12-15 152.500000 1064.189941"""

[Python] Plotting multiple stocks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.