Hello Everyone ! In this tutorial I will be showing you, how to plot Financial stock market data using Bokeh library and Pandas Data reader in python.The modules that we are going to use for this tutorial are pandas,pandas data reader and bokeh. So in case you have not installed these packages, please type the below line in your windows prompt.
pip install pandas pandas-datareader bokeh
Now since the libraries are installed lets import these packages in our program app.py like this
import pandas as pd import pandas_datareader as pdr from bokeh.plotting import figure,show,output_file from datetime import datetime
Our libraries have been imported and now we can start using them in our program. Please note that this program uses the Yahoo API
and we monitors the data for the company APPLE
(you are free to play with these parameters).
start=datetime(2017,10,26) end=datetime(2017,11,26) data=pdr.get_data_yahoo('AAPL',start,end)
Now that our data has been fetched we can proceed with some pre-processing with it to depict the visuals in an attractive and serialized manner.
data_sorted=data.sort_index(axis=0,ascending=True) open_list=list(data_sorted['Open']) close_list=list(data_sorted['Close']) date_list=list(data_sorted.index) date_time=[datetime.strptime(str(d),'%Y-%m-%d %H:%M:%S').date() for d in date_list]
At this point we are done with our data pre-processing and now we can take one step to the podium and plot our bokeh graph.
p=figure(x_axis_type='datetime',plot_width=800,plot_height=500,title="Financial Analysis of Apple",tools="", toolbar_location=None) p.circle(date_time,open_list,legend="Open Price",size=6,color="green", alpha=0.5) p.line(date_time,open_list,legend="Open Price",color="green", alpha=0.5) p.circle(date_time,close_list,legend="Close Price",size=6,color="red", alpha=0.5) p.line(date_time,close_list,legend="Close Price",color="red", alpha=0.5) p.legend.location = "bottom_right" show(p) output_file('graph.html')
We are done over here and we can now run this program using the command python app.py
in our command prompt.To view the result please open the generated .html
file in the browser. Enjoy !