The world’s leading publication for data science, AI, and ML professionals.

Interactive Visualization with Plotly and Datapane

Get more out of your visualizations with Plotly and Datapane

Interactive Candlestick Plot in Plotly (Image by Author)
Interactive Candlestick Plot in Plotly (Image by Author)

Visualization is a critical aspect of any analysis. But static plots limit our ability to understand data. Plotly and Datapane address this issue.

This article will show how to create interactive candlestick time-series plots to visualization the price of bitcoin. However, the code provided is easily adapted to support other time-series data.

The article is broken down into several sections:

  1. Extracting Data with Alpha Vantage
  2. Comparison of Static Plots in Matplotlib
  3. Embedding Plots with Plotly and Datapane
  4. Interactive Plots in Plotly with Advanced Features

Alpha Vantage

Alpha vantage is a financial services API that supports several programming languages, including Python, NodeJS, PDP, C#/.Net and many other languages. A robust open-source community has developed over 600 libraries for Alpha Vantage across these programming languages.

Alpha Vantage enables programmers to extract the intraday pricing of various equities and cryptocurrencies. The API is easy to use and allows for a great deal of flexibility. As a result, it is ideal for those wanting to back-test machine learning algorithms on large volumes of data, create visualizations, and perform financial analysis.

If you’re interested in using Data Science for finance, Alpha Vantage is the perfect tool.

Requirements

The tool requires a personalized API key to pull data. This key is available after signing up with a free account, but the number of calls is limited in the free tier for apparent reasons.

The API key you receive allows up to 5 requests per minute and up to 500 per day – more than enough to experiment with a large amount of financial data.

However, if you do require more financial data, consider the premium option. This option also allows for additional historical data. This option may be preferable for users who want to back-test their models on more data.

Extracting Data

For this article, I will be using the API within Python. The main functionality you will be using is likely the intraday function with either equities or crypto.

Next, you can specify the price in the currency of your choice using the relevant currency code. Then, the data interval is set with the interval command where the options are every minute, 5, 15, 30, or 60 minutes.

Finally, the output size dictates how much data you want to receive. Here I am using the full output size to obtain as much information as I can.

Make sure you replace the API_KEY variable with your API key.

The API returns both the meta-information as well as price information. The price information includes the open, close, high, and low prices.


Static Plots with Matplotlib

The most common plotting library in python is probably Matplotlib. It is a staple of many python tutorials and integrated into many packages. However, it has some clear disadvantages. The first is that you have to specify the axis range. While this does not initially seem like a prominent detractor of Matplotlib, it becomes an issue when more volatile data.

When a time series varies, you are restricted to choosing a short period to observe in detail or looking at a more significant period and viewing the overall trends.

mplfinance

I’ve showcased this by using the ‘mplfinance’ package. This package is built on top of Matplotlib and easily creates candlestick plots. While easy to set up and use, the axis range is set to the total range of the dataframe passed to the function.

Matplotlib-based Static Plot (Figure by Author)
Matplotlib-based Static Plot (Figure by Author)

To properly investigate different periods of data, you have to recreate the entire plot. While the focus of financial data is usually on the most recent data, drilling down into the historical data is crucial for back-testing algorithms. Only by closely observing the data are you able to view scenarios where your algorithms break down.


Embedding Plots with Datapane

Plotly is a beautiful library that provides interactive plots by default. However, simply inserting these plots into an article does not automatically allow for interactivity.

Instead, the plots can be hosted using Datapane.

Datapane is an open-source Python library that allows users to build interactive plots quickly. In addition, Datapane offers Datapane Studio, which is free and will enable you to upload public and private reports.

With the free tier of Datapane, you are allowed five private reports with email sharing, unlimited public report hosting, and social embedding API.

The social embedding API is used for this article to showcase the Plotly charts.

Note: Plotly does allow for write to html so that the interactive functionality of the plot persists. This option is great when sending plots to others when you want to keep the interactive aspect intact.

Embedding Plots

To host a chart on Datapane. First install Datapane. Next, login into Datapane using your token received during sign up. Something like this:

Simple create a Plotly chart and upload the report to Datapane with the Datapane report function. Here the date slider is added automatically to the plot since the index is a DateTime type. If you see DateTime values along the x-axis, the type is a string, and the date slider cannot be generated.

To make sure the reports are correctly integrated into Medium, you must set up an integration token. Then save this token to your Datapane account. A simple walkthrough is found here:

Export to Medium


Interactive Plots with Plotly

By default, Plotly allows a user to adjust the axis ranges of a plot dynamically. Additionally, the default plots allow for zooming, saving plots, adding or removing traces, resetting plots with double click, and a spike line toggle.

While the chart above is undoubtedly an improvement, there are some limitations.

The first is that while you can zoom in on multiple periods, the y axis stays constant. Unfortunately, this property of the default charts makes the candlestick charts fairly meaningless, as the candles flatten.

After some digging, I’ve discovered that you can update the y-axis dynamically on update. This update requires a few changes to the previous plot.

First is changing the graph object import from _plotly.graph_objects to plotly_graph_obs. Next is initializing your figure object as a FigureWidget instead of just a Figure. This import requires that ipywidgets_ is installed.

The second change is the removal of date breaks. While this is not required for Bitcoin, other financial equities have gaps in their time series. This pattern is due to market closures and can obscure the plots.

To avoid the issue, the dates on which no value exists are removed. First, a list of dates is generated for the entire range to prevent this issue. Then the dates which no value exists are dropped. The axis is then updated to reflect the change.

I’ve made the last changes to improve the candlestick plots: colouring (using a seaborn theme), format changes, and date slider buttons. These are more of a quality of life improvement. As the date slider can be quite cumbersome, the date buttons simplify selecting date ranges.

The buttons follow a standard format. The ‘step’ is specified as a DateTime frequency such as minute, hour, day, month, or year. Then the ‘count’ is set to dictate the size of the range. These buttons can select data from the start or end of the DateTime plot, support custom labels and custom date ranges.

Note Datapane does not support uploading FigureWidgets, and chart-studio (chart hosting from Plotly) does not provide a kernel to update the y-axis values on date selection. However, you can see the dynamic y-axis changes when running the chart within a Jupyter notebook.


Final Thoughts

Visualization is used throughout a data science project when the stakeholders require some tangible output from the analysis or initial data investigation.

However, if a visualization tool is too rigid, you risk missing some critical insights into your data. Interactive plots ensure that the exciting aspects of your data are readily observable.

In this article, bitcoin data is used. But it should be clear that the benefits of interactive plots apply to many different areas of data science.

These plots help convey stories, drive home messages, and help us investigate our data.

Plotly provides an out of the box solution to plot interactivity. Combined with Datapane you can send fully interactive plots to relevant stakeholders and reveal the intricacies of your data.


If you’re interested in reading articles about novel data science tools and understanding machine learning algorithms, consider following me on Medium.

If you’re interested in my writing and want to support me directly, please subscribe through the following link. This link ensures that I will receive a portion of your membership fees.

Join Medium with my referral link – Zachary Warnes


Related Articles