Matplotlib Finance (mplfinance)
mplfinance is a Python library built on Matplotlib and Pandas for the visualization and visual analysis of financial data. It specializes in generating highly customizable candlestick, OHLC, and Renko charts, often with integrated volume, moving averages, and other technical indicators. It is currently in active development, releasing frequent beta versions (latest is 0.12.10b0) with ongoing feature enhancements and bug fixes.
Warnings
- gotcha mplfinance expects input data as a Pandas DataFrame with a DatetimeIndex and specific column names (case-sensitive): 'Open', 'High', 'Low', 'Close', and optionally 'Volume'. Incorrect indexing or column naming will lead to errors.
- deprecated Older versions of mplfinance (prior to 0.12.9b7) may issue deprecation warnings or encounter compatibility issues when run with recent versions of Matplotlib or Pandas.
- gotcha Direct manipulation of Matplotlib Axes objects returned by `mpf.plot()` can be challenging due to mplfinance's internal panel structure. For overlaying custom data series or indicators, the `addplot` kwarg is the idiomatic approach.
- gotcha mplfinance frequently releases beta versions (e.g., `0.12.10b0`) which may introduce minor API changes or new features that are refined rapidly. While generally stable, users should monitor release notes carefully.
Install
-
pip install mplfinance -
pip install --pre mplfinance
Imports
- mpf
import mplfinance as mpf
Quickstart
import mplfinance as mpf
import pandas as pd
import numpy as np
# Create dummy OHLCV data with DatetimeIndex
dates = pd.date_range('2023-01-01', periods=50, freq='D')
np.random.seed(42)
open_price = np.random.rand(50) * 100 + 100
close_price = open_price + np.random.randn(50) * 5
high_price = np.maximum(open_price, close_price) + np.random.rand(50) * 2
low_price = np.minimum(open_price, close_price) - np.random.rand(50) * 2
volume = np.random.rand(50) * 1000000
df = pd.DataFrame({
'Open': open_price,
'High': high_price,
'Low': low_price,
'Close': close_price,
'Volume': volume
}, index=dates)
# Plot a basic candlestick chart with volume
fig, axes = mpf.plot(df,
type='candle',
style='yahoo',
volume=True,
title='Sample Candlestick Chart',
ylabel='Price',
ylabel_lower='Volume',
returnfig=True
)
# To display the plot in a non-interactive environment or save it
# fig.savefig('candlestick_chart.png')
# import matplotlib.pyplot as plt
# plt.show() # Uncomment for interactive display