Data visualization in Python using Bokeh

Data visualization using Bokeh

Python Data visualization using Bokeh by Milind Paradkar

A picture is worth a thousand words or said a wise woman a hundred years ago. True to every word of the idiom, the beauty of visualization lies in how clearly it might convey multiple messages. Visualization of data is one of the key functions of a data scientist and decoding the visual messages is of primary importance to the algo trader. The patterns (both hidden and the obvious) are of utmost importance to the traders and analysts as they decide their trading strategy and next move based on these interpretations.

Python offers cool ways of creating appealing plots and graphics. Some of the popular packages include Matplotlib, Bokeh, Plotly and Seaborn. We are going to talk about one of these tools: Bokeh.

Bokeh for Python Data Visualization

Bokeh is a Python interactive visualization library that uses modern web browsers for presentation. Using Bokeh we can quickly create interactive plots, dashboards, and data applications with ease[1]

Bokeh’s ultimate objective is to give graceful looking and apt visual depictions of data in the form of D3.js. Bokeh is useful for all those who wish to quickly and easily create interactive plots, dashboards, and data applications. Let us see how Python Data Visualization is done using Bokeh.

Plot Types

The main plot types in Bokeh are:

Server App plots

These are connected to the Bokeh server, and the data can be updated which in turn updates the plot, and the UI.

Standalone plots

These plots do not use the Bokeh server. However, they still have many interactive tools and features, including linked panning, brushing, and hover inspectors.

Installing Bokeh

Installing Bokeh is simple and can be installed in python from PyPI (Python Package Index) using the following pip command:

pip install bokeh

Alternatively, Anaconda users can simply run the command:

conda install bokeh

This will install the most recent published Bokeh release from the Continuum Analytics Anaconda repository, along with all the dependencies.

Some of the Bokeh examples rely on sample data that is not included in the Bokeh GitHub repository or released packages, due to their size. Once Bokeh is installed, the sample data can be obtained by executing the following command in a Python interpreter:
data visualization in python using bokeh 1 code quantinsti

 

Getting Started in Bokeh

The architecture of Bokeh is such that high-level “model objects” (representing things like plots, ranges, axes, glyphs, etc.) are created in Python, and then converted to a JSON format that is consumed by the client library, BokehJS. BokehJS renders the visuals and handles the UI interactions for Bokeh plots and widgets in the browser.

Bokeh, that allows for Python data visualization, offers three main interfaces that include:

bokeh.models

A low-level interface which provides complete control over how Bokeh plots and Bokeh widgets are put together and configured. Most of the models are very simple, usually consisting of a few property attributes and no methods.

bokeh.plotting

A mid-level interface which provides a convenient way to create plots centered around glyphs. Glyphs are the basic visual building blocks of Bokeh plots, e.g. lines, rectangles, squares, wedges, patches, etc.

bokeh.charts

A high-level interface for quickly creating statistical charts. The interface includes chart types such as: Bar(), BoxPlot(), Histogram(), TimeSeries(), and many others.

There are various ways to generate output for Bokeh documents. The commonly used include:

  1. output_file – Generates simple standalone HTML documents for Bokeh visualizations
  2. output_notebook – Displays Bokeh visualizations inline in Jupyter notebook cells

These functions are most often used together with the show or save functions.

Let us now plot some charts which will demonstrate the ease and power of Bokeh plots. We will explain the bokeh functions, methods, and attributes as we take you through the charting examples illustrated below.

Know everything about algo trading

Example 1: NIFTY Sectoral Indices Performance

Let us start with this example by plotting the NIFTY Sectoral Indices performance over the last one year. The names of the different sectors and the corresponding yearly returns are contained in the lists; “factors” and “x”. We create a dot object using the figure function from bokeh.plotting, and apply the “segment”, and the “circle” methods to draw the lines and the circle at the end of the lines in the dot plot.

Since the returns include both positive as well as negative returns, we have used if else statement in a for loop, and have created a list (LineColor) wherein a “green” color is assigned to positive returns, and a “red” color for negative returns.

In the “output_file” function we have specified file type and file name. The “show” function displays the plot in the html format.

data visualization in python using bokeh 1 code quantinsti

 

python data visualization using bokeh 1 code quantinsti

You can use the toolbar at the top-right to zoom, pan, save, and reset the plot.

Example 2: NIFTY Media Stocks Performance

 You can see from the plot above, the Media Sector gained the most (approx.30%) in the last one year. Now let us visualize the gains clocked by the various stocks comprising the NIFTY Media Sector. The stock returns data is loaded using the read_csv function from pandas. Next, we use the Scatter function from bokeh.charts to create the ‘scatter’ object. We specify the stock names on the x-axis, and plot the returns on the y-axis. The “show” function displays the scatter plot in html format. You can also save the plot using the “save” function.

data visualization in python using bokeh 1 code quantinstipython data visualization using bokeh 1 code quantinsti

Example 3: Comparing TVTODAY Vs NIFTY series

What did you observe from the scatter plot?

We can see that TVTODAY stock was one of the top performing stocks in the NIFTY Media sector clocking more than 40% returns in the last one year. Let us now plot the TVTODAY price series and compare it with the NIFTY index. We load the TVTODAY and the NIFTY price series data using read_csv function from pandas. Using the figure function we create ‘n’ object, and plot the TVTODAY price series using the line method. Similarly, we plot the NIFTY time series.

To display the time series plots one below another we use the “column” function from bokeh.layouts and pass the ‘n’ and ‘p’ objects to the “column” function which is nested in the “show” function (Last line of the code). This will produce time series plots one below another.

quantinsti epat for bokeh plotting

quantinsti epat for bokeh plotting 

Example 4: TVTODAY Quarterly Y/Y Sales & Profit growth

 Comparing the two price series above, we can observe that TVTODAY stock saw a sharp upside move during the period Nov’15 – Jan’16, while NIFTY trended downwards during the same time frame. Can the TVTODAY stock fundamentals explain the sharp upside? We looked at the sales and profit growth numbers for TVTODAY stock, and plotted a bar plot covering the last four quarters.

For plotting, we create a dictionary which contains the fundamental data (sales and net profit) for the last four quarters, and apply it to the “Bar” function from bokeh.charts.

quantinsti-epat-for-bokeh-plotting quantinsti-epat-for-chart-analsys

From the bar plot we deduce that TVTODAY saw good results for the Sep’15, Dec’15, and the Mar’16 quarters. Also, there was favorable news for the company in the early December month, which allowed for NRI investment in the company. The good quarterly results and the favorable news gives some explanation for the upside move seen during Nov’15 – Jan’16.

Here is a great article to read on implementing Python codes in Interactive Brokers API.

To conclude

As illustrated by the examples above, Bokeh is very user-friendly, feature rich, and can be used to create interactive charts easily. The extensive documentation of various interfaces, functions, methods on its site makes it easy for the beginners to learn bokeh. You can always go to Bokeh’s site and know more about it.

Next Step

Visualization of data on Python is just one of the elements covered in the vast domain of Algorithmic Trading. Know why Python algorithmic trading is preferred choice among traders. To understand the patterns, one must be well-versed in the basics. Want to know more about Algorithmic trading? You should click here and check out more about Algorithmic Trading.

                  

 

Leave a Reply

Your email address will not be published. Required fields are marked *