Python Bokeh – Visualizing Stock Data - GeeksforGeeks

03 Jul, 2020
Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity.Bokeh can be used to visualize stock market data. Visualization is be done using the plotting module. Here we will be using the sample stock datasets given to us by Bokeh.
To download the sample datasets run the following command on the command line :
bokeh sampledata
Alternatively, we can also execute the following Python code :
import bokeh
bokeh.sampledata.download()

In the sample data provided by Bokeh, there are datasets of the stocks of the following companies :
AAPL which is Apple
FB which is Facebook
GOOG which is Google
IBM which is International Business Machines
MSFT which is Microsoft Corporation
All these datasets are available as CSV files. Below is a glimpse into the IBM.csv file :
Date        Open    High    Low    Close    Volume        Adj Close
01-03-2000    102    105.5    100.06    100.25    10807800    84.48
02-03-2000    100.5    105.44    99.5    103.12    11192900    86.9
03-03-2000    107.25    110    106.06    108    10162800    91.01
06-03-2000    109.94    111    101    103.06    10747400    86.85
07-03-2000    106    107    101.69    103    10035100    86.8

The file contains the stock data between the years 2000 and 2013 with over 3000 entries.
We will be plotting a line graph which will track the closing price of the stocks between the years 2000 and 2013 of all the 5 available companies.
Import the required modules :numpyfigure, output_file and show from bokeh.plottingAAPL, FB, GOOG, IBM and MSFT from bokeh.sampledata.stocksInstantiate a figure object with the title and axis types.Give the names to x-axis and y-axis.Plot line graphs for all the 5 companies.Display the model.
Import the required modules :numpyfigure, output_file and show from bokeh.plottingAAPL, FB, GOOG, IBM and MSFT from bokeh.sampledata.stocks
numpy
figure, output_file and show from bokeh.plotting
AAPL, FB, GOOG, IBM and MSFT from bokeh.sampledata.stocks
Instantiate a figure object with the title and axis types.
Give the names to x-axis and y-axis.
Plot line graphs for all the 5 companies.
Display the model.
# importing the modulesimport numpy as npfrom bokeh.plotting import figure, output_file, showfrom bokeh.sampledata.stocks import AAPL, FB, GOOG, IBM, MSFT   # the file to save the modeloutput_file("gfg.html")   # instantiating the figure objectgraph = figure(x_axis_type = "datetime", title = "Stock Closing Prices")   # name of the x-axisgraph.xaxis.axis_label = 'Date'   # name of the y-axisgraph.yaxis.axis_label = 'Price (in USD)'   # plotting the line graph for AAPLx_axis_coordinates = np.array(AAPL['date'], dtype = np.datetime64)y_axis_coordinates = AAPL['adj_close']color = "lightblue"legend_label = 'AAPL'graph.line(x_axis_coordinates,        y_axis_coordinates,        color = color,        legend_label = legend_label)   # plotting the line graph for FBx_axis_coordinates = np.array(FB['date'], dtype = np.datetime64)y_axis_coordinates = FB['adj_close']color = "black"legend_label = 'FB'graph.line(x_axis_coordinates,        y_axis_coordinates,        color = color,        legend_label = legend_label)   # plotting the line graph for GOOGx_axis_coordinates = np.array(GOOG['date'], dtype = np.datetime64)y_axis_coordinates = GOOG['adj_close']color = "orange"legend_label = 'GOOG'graph.line(x_axis_coordinates,        y_axis_coordinates,        color = color,        legend_label = legend_label)   # plotting the line graph for IBMx_axis_coordinates = np.array(IBM['date'], dtype = np.datetime64)y_axis_coordinates = IBM['adj_close']color = "darkblue"legend_label = 'IBM'graph.line(x_axis_coordinates,        y_axis_coordinates,        color = color,        legend_label = legend_label)   # plotting the line graph for MSFTx_axis_coordinates = np.array(MSFT['date'], dtype = np.datetime64)y_axis_coordinates = MSFT['adj_close']color = "yellow"legend_label = 'MSFT'graph.line(x_axis_coordinates,        y_axis_coordinates,        color = color,        legend_label = legend_label)   # relocating the legend table to # avoid abstruction of the graphgraph.legend.location = "top_left"   # displaying the modelshow(graph)
Output :
Data Visualization
Python-Bokeh
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Python Dictionary
Taking input in Python
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python