Financial charts and visuals with Plotly in Python

Professional financial charts with less-code using Plotly in Python



Plotly for Visualization


One of the most popular packages used for interactive visualizations is Plotly. Plotly is built on top of python and enables data scientists to produce professional and great-looking plots with less-code. It became popular because of its extensive category of plots which can be produced in no-time. The categories of plots include basic charts, statistical charts, ML and AI charts, scientific charts, and financial charts.

In this article, I’m going to walk you through the process of creating interactive and professional financial charts in Plotly with python. We will also explore yahoo’s API for pulling historical stock data which we will be using for visualizations. Let’s get started!


Importing packages


Our primary packages include pandas for data processing, pandas DataReader for pulling the historical stock data, Datetime to deal with dates, finally, Plotly and its dependencies for interactive visualizations. Follow the code to import the primary packages into our python

environment.


Python Implementation:


Our next process is going to be pulling the historical stock data for visualizations using the pandas DataReader package.


Pulling Stock Data


For our visualizations, we are going to pull six companies’ historical data namely Facebook, Amazon, Apple, Netflix, Google, and Microsoft using yahoo’s API. Let’s pull the data in python!


Python Implementation:


Firstly, we have defined two variables specifying the start and end date of our data. Next, using the pandas DataReader package, we have pulled the historical data of the companies. Finally, we stored only the close price data of companies in the ‘stocks_close’ variable. Now, we are ready to do visualizations on our stock data.


Area Chart


Often called ‘Mountain’ charts, area charts are a more simplified interpretation of standard line charts. They plot closing prices over a given period, and the area beneath the line is shaded. Follow the code to create an area chart with Plotly in python.


Python Implementation:


Output:

The above-produced area chart is very basic when compared to Plotly’s efficient functions. Now, let’s create a customized area chart by making use of Plotly’s powerful functions. Follow the code to create a customized area chart with Plotly in python.


Python Implementation:


Output:

Candlestick Chart


A candlestick is a type of price chart used in technical analysis that displays the high, low, open, and closing prices of a security for a specific period. Candlestick charts are used by traders to determine possible price movements based on past patterns. Candlesticks are useful when trading as they show four price points (open, close, high, and low) throughout the period of time the trader specifies. Many algorithms are based on the same price information shown in candlestick charts. Trading is often dictated by emotion, which can be read in candlestick charts. Follow the code to produce a candlestick chart with Plotly in python.


Python Implementation:


Output:

Like the area chart, candlestick charts can also be tuned and customized to look more professional. Follow the code to produce a customized candlestick chart with Plotly in python.


Python Implementation:


Output:

OHLC Chart


An OHLC chart is a type of bar chart that shows open, high, low, and closing prices for each period. OHLC charts are useful since they show the four major data points over a period, with the closing price being considered the most important by many traders. The chart type is useful because it can show increasing or decreasing momentum. When the open and close are far apart it shows strong momentum, and when they open and close are close together it shows indecision or weak momentum. The high and low show the full price range of the period, useful in assessing volatility. Follow the code to produce an OHLC chart with Plotly in python.


Python Implementation:


Output:

Now, let’s customize the default OHLC chart using Plotly’s functions to make the plot more professional and great-looking. Follow the code to create a customized OHLC chart with Plotly in python.


Python Implementation:


Output:

Bullet Chart


A Bullet Chart is a variation of a Bar Chart designed to compare a single, primary measure to one or more other measures to enrich its meaning, and displays it in the context of qualitative ranges of performance. The qualitative ranges are displayed as blocks of one hue but with varying intensity, making them discernible by those who are color blind and to restrict the use of colors on the dashboard to a minimum. We are going to use a bullet chart to represent a stock’s price and day range. Follow the code to create a bullet chart with Plotly in python.


Python Implementation:


Output:

Customized Bullet Chart Python Implementation:


Output:

Gauge Chart


A radial gauge chart has a circular arc, which displays a single value to estimate progress towards a goal. The bar shows the target value, and the shading represents the progress toward that goal. Gauge charts, known as speedometer charts as well. Using a gauge chart, we are going to represent a stock’s day range just like how we did use the bullet chart. Follow the code to produce a gauge chart with Plotly in python.


Python Implementation:


Output:

Final Thoughts!


In this article, we have seen how to use Plotly for financial charts in python. Starting with area charts we have learned to create five types of financial visualizations with Plotly in python. Apart from this, there are also other financial visualizations that are used to plot data other than stocks but, it is worthwhile. Also, we have customized our charts just a little. There are many more ways and functions available in Plotly to customize and beautify the charts. You can learn and explore many more types of charts and tools on Plotly’s python documentation page. And, if you forgot to follow the coding parts for any of the visualizations, don’t worry, I’ve provided the source code for all the visualizations at the end. With that, we came to the end of the article.


Happy Visualizing!


Full code:




501 views5 comments