Candlestick chart in plotly

Candlestick chart in plotly with Candlestick

A candlestick chart displays the open, high, low and close price (OHLC) of an asset for each period, which is why it is mostly used for financial data. Use the Candlestick class from plotly.graph_objects, passing an array of dates to x and the four price series to open, high, low and close.

import plotly.graph_objects as go
import pandas as pd

# Sample data
dates = pd.date_range("2024-01-01", periods = 10, freq = "D")
open_p = [100, 102, 101, 104, 103, 105, 107, 106, 108, 110]
high_p = [103, 104, 105, 106, 106, 108, 109, 109, 111, 113]
low_p = [99, 100, 100, 102, 101, 104, 105, 104, 107, 108]
close_p = [102, 101, 104, 103, 105, 107, 106, 108, 110, 109]

fig = go.Figure(data = [go.Candlestick(
    x = dates, open = open_p, high = high_p, low = low_p, close = close_p)])

fig.show()

Colors

The color of the rising and falling candles is set independently through the increasing and decreasing arguments, each accepting a dict with a line_color key (and optionally fillcolor).

import plotly.graph_objects as go
import pandas as pd

# Sample data
dates = pd.date_range("2024-01-01", periods = 10, freq = "D")
open_p = [100, 102, 101, 104, 103, 105, 107, 106, 108, 110]
high_p = [103, 104, 105, 106, 106, 108, 109, 109, 111, 113]
low_p = [99, 100, 100, 102, 101, 104, 105, 104, 107, 108]
close_p = [102, 101, 104, 103, 105, 107, 106, 108, 110, 109]

fig = go.Figure(data = [go.Candlestick(
    x = dates, open = open_p, high = high_p, low = low_p, close = close_p,
    increasing_line_color = "#55A868", decreasing_line_color = "#C44E52")])

fig.show()

Removing the range slider

By default plotly adds a range slider below the chart. Remove it by setting xaxis_rangeslider_visible to False with update_layout.

import plotly.graph_objects as go
import pandas as pd

# Sample data
dates = pd.date_range("2024-01-01", periods = 10, freq = "D")
open_p = [100, 102, 101, 104, 103, 105, 107, 106, 108, 110]
high_p = [103, 104, 105, 106, 106, 108, 109, 109, 111, 113]
low_p = [99, 100, 100, 102, 101, 104, 105, 104, 107, 108]
close_p = [102, 101, 104, 103, 105, 107, 106, 108, 110, 109]

fig = go.Figure(data = [go.Candlestick(
    x = dates, open = open_p, high = high_p, low = low_p, close = close_p)])
fig = fig.update_layout(xaxis_rangeslider_visible = False)

fig.show()

Title

Add a title the same way as in any other plotly figure, with update_layout.

import plotly.graph_objects as go
import pandas as pd

# Sample data
dates = pd.date_range("2024-01-01", periods = 10, freq = "D")
open_p = [100, 102, 101, 104, 103, 105, 107, 106, 108, 110]
high_p = [103, 104, 105, 106, 106, 108, 109, 109, 111, 113]
low_p = [99, 100, 100, 102, 101, 104, 105, 104, 107, 108]
close_p = [102, 101, 104, 103, 105, 107, 106, 108, 110, 109]

fig = go.Figure(data = [go.Candlestick(
    x = dates, open = open_p, high = high_p, low = low_p, close = close_p)])
fig = fig.update_layout(title = "Stock price", xaxis_rangeslider_visible = False)

fig.show()

Hiding weekend gaps

With daily stock data, weekends have no trading and create empty gaps on the X-axis. Remove them by adding a rangebreak for Saturdays and Sundays.

import plotly.graph_objects as go
import pandas as pd

# Sample data (business days only)
dates = pd.bdate_range("2024-01-01", periods = 10)
open_p = [100, 102, 101, 104, 103, 105, 107, 106, 108, 110]
high_p = [103, 104, 105, 106, 106, 108, 109, 109, 111, 113]
low_p = [99, 100, 100, 102, 101, 104, 105, 104, 107, 108]
close_p = [102, 101, 104, 103, 105, 107, 106, 108, 110, 109]

fig = go.Figure(data = [go.Candlestick(
    x = dates, open = open_p, high = high_p, low = low_p, close = close_p)])
fig = fig.update_xaxes(rangebreaks = [dict(bounds = ["sat", "mon"])])

fig.show()
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon

See also