OHLC Charts With Python Libraries
By: Alex Mitrani
Tags: Data Visualization, Data Science, Plotly, Matplotlib, Python

There are several graphing libraries available to Python coders. Two of the most popular ones are Matplotlib and Plotly. Picking the right library depends on your situation. I recreated the OHLC (open-high-low-close) chart in both libraries to highlight these differences and included the code below.

Matplotlib

Matplotlib is the first graphing and plotting library I learned to use when I began coding in Python. It is a highly customizable library which is both its strength and weakness. You can make powerful visualizations for your specific purposes however there is a high amount of customizable code you have to adjust.

Matplotlib’s finance module has been deprecated and moved to the mpl_finance module. Many tutorials online do not reflect this change, however, the patch is relatively easy to implement with the PIP package installer tool. I created a new environment specifically for this exercise incase I did not want this module in my main environment. The module is located on Matplotlib’s GitHub profile as a zip file, the command is as follows:

!pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

I decided to plot the year-to-date (YTD) OHLC chart of the Campbell Soup Company (CPB), ending on November 21, 2019. The data was downloaded from Yahoo Finance.

Plotting the YTD OHLC chart for the Campbell Soup Company with Matplotlib. The data came from Yahoo Finance and the tutorial was made by Harrison Kinsley.

The main difficulties were unraveling the rows of the pandas data frame and converting the date strings to Matplotlib’s floating point number format. My dates came in string format so I was able to use Matplotlib’s datestr2num() method.

The Campbell Soup Company YTD chart created with Matplotlib

Plotly

Plotly offers many products including an open source graphing library for the Python language. Plotly has numerous interactive charts available including several specific to finance. I recreated the Campbells OHLC YTD chart below.

The code for a Plotly OHLC chart with a rangeslider for the Campbell Soup Company

Plotly works well with a Pandas data frame. The columns can be used directly without the need to unravel them. There are several options available within the OHLC chart section of Plotly. This code produced an interactive chart with a rangeslider and hovertext. The rangeslider allows you to zoom in on a specific section of time and the hover text provides more information on that data point. A static image of this chart is below:

An image of the interactive Campbell OHLC chart produced with Plotly. The rangeslider below allows you to zoom in on a time period.

A zoomed in version of the Plotly chart with a rangeslider and hovertext window open.

Growth Rates

Using Pandas and Plotly I created a couple growth rate charts. Pandas’ pct_change() method calculates the percentage change between row entries. I utilized Plotly’s line and scatter functions to create this chart, the code is below:

The code for a Plotly growth rate line chart

You can also add multiple traces to the plot. I compared Campbells’ growth rate to the S&P 500 Index’s growth rate:

The Campbell Soup Company YTD growth chart with Plotly

The Campbell Soup Company and S&P 500 growth chart with Plotly

Comparison & Summary

Both Matplotlib and Plotly have their place in a data scientist’s toolkit. Matplotlib is highly customizable but requires much more coding and data preparation, which is time consuming. Plotly creates powerful interactive visualizations with relatively little code. I preferred to use Plotly to make the OHLC charts. It’s up to you to decide which library you need for your purposes.