Matplotlib Finance (mplfinance)

0.12.10b0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart generates dummy OHLCV data using Pandas and NumPy, then plots a basic candlestick chart with volume using `mpf.plot()`. It demonstrates the required DataFrame format (DatetimeIndex, specific column names) and common plotting parameters like `type`, `style`, and `volume`. The `returnfig=True` argument allows access to the underlying Matplotlib Figure and Axes objects for further customization or saving.

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

view raw JSON →