plotly Candlestick Chart in Python (3 Examples)
Hi! This tutorial will show you how to build a plotly candlestick chart in Python.
Here is an overview:
Let’s get into the Python code!
Install & Import plotly & pandas Library
To install and import plotly and pandas, run the lines of code below in your preferred Python programming IDE or editor:
# install plotly & pandas pip install plotly pandas # import plotly & pandas import plotly.graph_objects as go import pandas as pd
When it comes to DataFrame manipulation in Python, pandas is the go-to library. We need pandas in this project to be able to read data from an external source.
So, with plotly and pandas installed and imported into our Python programming environment, we can now build a candlestick chart.
First, though, we need to get the sample dataset that we will visualize. This is where pandas comes in.
Get Sample Data
We will fetch the data from GitHub using the line of code below:
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
You can then preview the first 10 rows of the data by running:
df.head(10) # Date AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted dn mavg up direction # 0 2015-02-17 127.489998 128.880005 126.919998 127.830002 63152400 122.905254 106.741052 117.927667 129.114281 Increasing # 1 2015-02-18 127.629997 128.779999 127.449997 128.720001 44891700 123.760965 107.842423 118.940333 130.038244 Increasing # 2 2015-02-19 128.479996 129.029999 128.330002 128.449997 37362400 123.501363 108.894245 119.889167 130.884089 Decreasing # 3 2015-02-20 128.619995 129.500000 128.050003 129.500000 48948400 124.510914 109.785449 120.763500 131.741551 Increasing # 4 2015-02-23 130.020004 133.000000 129.660004 133.000000 70974100 127.876074 110.372516 121.720167 133.067817 Increasing # 5 2015-02-24 132.940002 133.600006 131.169998 132.169998 69228100 127.078049 111.094869 122.664834 134.234798 Decreasing # 6 2015-02-25 131.559998 131.600006 128.149994 128.789993 74711700 123.828261 113.211918 123.629667 134.047415 Decreasing # 7 2015-02-26 128.789993 130.869995 126.610001 130.419998 91287500 125.395469 114.165299 124.282333 134.399367 Increasing # 8 2015-02-27 130.000000 130.570007 128.240005 128.460007 62014800 123.510987 114.966848 124.842667 134.718485 Decreasing # 9 2015-03-02 129.250000 130.279999 128.300003 129.089996 48096700 124.116706 115.877090 125.403667 134.930243 Decreasing
Now that we have loaded the dataset, we can build the plotly candlestick visualization.
Example 1: Build Candlestick Chart Chart
In this first example, we will build a basic candlestick chart:
fig = go.Figure(data=[go.Candlestick(x=df['Date'], open=df['AAPL.Open'], high=df['AAPL.High'], low=df['AAPL.Low'], close=df['AAPL.Close'])]) fig.show()
In the above example, we passed an array to the go.Figure() function containing the go.Candlestick() function, wherein we passed the DataFrame df
specifying the Date
column as the x axis.
Then, we also passed the respective columns in the DataFrame to the open
, high
, low
, and close
arguments in the go.Candlestick()
function.
Finally, we displayed the chart using fig.show().
Example 2: Remove Range Slider from Candlestick Chart
In this second example, we will remove the range slider from the candlestick chart:
fig = go.Figure(data=[go.Candlestick(x=df['Date'], open=df['AAPL.Open'], high=df['AAPL.High'], low=df['AAPL.Low'], close=df['AAPL.Close'])]) fig.update_layout(xaxis_rangeslider_visible=False) fig.show()
Here, we only added a line of code to remove the range slider. In the fig.update_layout() method, we defined the xaxis_rangeslider_visible =
argument as False in order to hide the range slider.
Example 3: Customize Chart Colors in Candlestick Chart
In this final example, we will customize the colors of the candlestick chart:
fig = go.Figure(data=[go.Candlestick( x=df['Date'], open=df['AAPL.Open'], high=df['AAPL.High'], low=df['AAPL.Low'], close=df['AAPL.Close'], increasing_line_color= 'blue', decreasing_line_color= 'yellow' )]) fig.show()
In the above example, we have changed the colors of the candlestick chart. To do so, all we had to do was to define the increasing_line_color =
and decreasing_line_color =
arguments in the go.Candlestick() function with the colors we want, which in this case were blue and yellow.
That way, we have customized the colors of the candlestick chart.
Video, Further Resources & Summary
Do you need more explanations on how to build plotly candlestick charts in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to build plotly candlestick charts in Python.
The YouTube video will be added soon.
With that, we have demonstrated how to build a plotly candlestick chart in Python. Furthermore, you could have a look at some of the other interesting plotly in Python tutorials on Statistics Globe:
- plotly Bubble Chart in Python (3 Examples)
- Customize Layout of plotly Graph in Python (3 Examples)
- plotly Contour Plot in Python (4 Examples)
- plotly Table in Python (4 Examples)
- plotly Map in Python (3 Examples)
- Introduction to Python Programming
This post has shown how to build plotly candlestick charts in Python. I hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.