GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PYTHON CHARTS
  • matplotlib
  • seaborn
  • plotly
  • pandas
  • colors
  • COLORS
  • COLOR PALETTES
  • R CODER
  • R CHARTS
  • Contact
  • Español

This view

  • Original page
  • New address

GeezerVison is showing python-charts.com — Candlestick chart in Python with mplfinance

Search for a graph

HOME EVOLUTION CANDLESTICK CHART MPLFINANCE

Candlestick chart in Python with mplfinance

Library

mplfinance

Author

Daniel Goldfarb

Candlestick charts represent the evolution of the price of an asset: each candle shows the opening, the highest, the lowest and the closing price of a period. The mplfinance library, built on matplotlib, creates them from a pandas data frame with a date index and the columns Open, High, Low, Close and, optionally, Volume.

Candlestick chart with mpf.plot

Pass the data frame to mpf.plot and set type = "candle".

import numpy as np
import pandas as pd
import mplfinance as mpf

rng = np.random.default_rng(21)
close = 50 + rng.normal(0, 1, 60).cumsum()
df = pd.DataFrame({"Open": close + rng.normal(0, 0.5, 60),
                   "Close": close,
                   "Volume": rng.integers(1000, 5000, 60)},
                  index = pd.bdate_range("2025-03-03", periods = 60))
df["High"] = df[["Open", "Close"]].max(axis = 1) + rng.uniform(0, 1, 60)
df["Low"] = df[["Open", "Close"]].min(axis = 1) - rng.uniform(0, 1, 60)

mpf.plot(df, type = "candle", figsize = (4, 4))

Candlestick chart in Python with mplfinance

OHLC bars and line

Other values of type are "ohlc" (bars with ticks for the opening and closing prices), "line" and "renko".

import numpy as np
import pandas as pd
import mplfinance as mpf

rng = np.random.default_rng(21)
close = 50 + rng.normal(0, 1, 60).cumsum()
df = pd.DataFrame({"Open": close + rng.normal(0, 0.5, 60),
                   "Close": close,
                   "Volume": rng.integers(1000, 5000, 60)},
                  index = pd.bdate_range("2025-03-03", periods = 60))
df["High"] = df[["Open", "Close"]].max(axis = 1) + rng.uniform(0, 1, 60)
df["Low"] = df[["Open", "Close"]].min(axis = 1) - rng.uniform(0, 1, 60)

mpf.plot(df, type = "ohlc", figsize = (4, 4))

OHLC chart in Python with mplfinance

Volume

Set volume = True to add a panel with the traded volume below the candles.

import numpy as np
import pandas as pd
import mplfinance as mpf

rng = np.random.default_rng(21)
close = 50 + rng.normal(0, 1, 60).cumsum()
df = pd.DataFrame({"Open": close + rng.normal(0, 0.5, 60),
                   "Close": close,
                   "Volume": rng.integers(1000, 5000, 60)},
                  index = pd.bdate_range("2025-03-03", periods = 60))
df["High"] = df[["Open", "Close"]].max(axis = 1) + rng.uniform(0, 1, 60)
df["Low"] = df[["Open", "Close"]].min(axis = 1) - rng.uniform(0, 1, 60)

mpf.plot(df, type = "candle", volume = True, figsize = (4, 4))

Candlestick chart with volume in mplfinance

Moving averages

The mav argument adds simple moving averages of the number of periods you specify. Pass a tuple to add several.

import numpy as np
import pandas as pd
import mplfinance as mpf

rng = np.random.default_rng(21)
close = 50 + rng.normal(0, 1, 60).cumsum()
df = pd.DataFrame({"Open": close + rng.normal(0, 0.5, 60),
                   "Close": close,
                   "Volume": rng.integers(1000, 5000, 60)},
                  index = pd.bdate_range("2025-03-03", periods = 60))
df["High"] = df[["Open", "Close"]].max(axis = 1) + rng.uniform(0, 1, 60)
df["Low"] = df[["Open", "Close"]].min(axis = 1) - rng.uniform(0, 1, 60)

mpf.plot(df, type = "candle", mav = (5, 20), volume = True,
         style = "yahoo", figsize = (4, 4))

Candlestick chart with moving averages in mplfinance

Styles and colors

mplfinance includes several styles, such as "yahoo", "charles", "nightclouds" or "binance" (see them all with mpf.available_styles()). To choose the colors of the candles create a style with make_marketcolors and make_mpf_style.

import numpy as np
import pandas as pd
import mplfinance as mpf

rng = np.random.default_rng(21)
close = 50 + rng.normal(0, 1, 60).cumsum()
df = pd.DataFrame({"Open": close + rng.normal(0, 0.5, 60),
                   "Close": close,
                   "Volume": rng.integers(1000, 5000, 60)},
                  index = pd.bdate_range("2025-03-03", periods = 60))
df["High"] = df[["Open", "Close"]].max(axis = 1) + rng.uniform(0, 1, 60)
df["Low"] = df[["Open", "Close"]].min(axis = 1) - rng.uniform(0, 1, 60)

colors = mpf.make_marketcolors(up = "#2A9D8F", down = "#E76F51", inherit = True)
style = mpf.make_mpf_style(marketcolors = colors, gridstyle = ":")

mpf.plot(df, type = "candle", style = style, figsize = (4, 4))

Custom candle colors in mplfinance

Title and labels

import numpy as np
import pandas as pd
import mplfinance as mpf

rng = np.random.default_rng(21)
close = 50 + rng.normal(0, 1, 60).cumsum()
df = pd.DataFrame({"Open": close + rng.normal(0, 0.5, 60),
                   "Close": close,
                   "Volume": rng.integers(1000, 5000, 60)},
                  index = pd.bdate_range("2025-03-03", periods = 60))
df["High"] = df[["Open", "Close"]].max(axis = 1) + rng.uniform(0, 1, 60)
df["Low"] = df[["Open", "Close"]].min(axis = 1) - rng.uniform(0, 1, 60)

mpf.plot(df, type = "candle", style = "nightclouds", figsize = (4, 4),
         title = "Stock price", ylabel = "Price ($)")

Candlestick chart with title in mplfinance

To create an interactive candlestick chart see the candlestick chart in plotly tutorial.

R CODER website
Data visualization

Data visualization

A practical introduction

Buy on Amazon
Storytelling with Data

Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon

See also

Candlestick chart in plotly

Candlestick chart in plotly

Gantt chart in plotly

Gantt chart in plotly

Line chart in pandas

Line chart in pandas

Area chart in plotly

Area chart in plotly

Streamgraph in matplotlib

Streamgraph in matplotlib

Step plot in matplotlib

Step plot in matplotlib

Line chart in plotly

Line chart in plotly

Area plot in matplotlib with fill_between

Area plot in matplotlib with fill_between

Calendar heatmap in matplotlib with calplot

Calendar heatmap in matplotlib with calplot

Area chart in pandas

Area chart in pandas

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.