mplfinance Documentation

repository·master·Indexed 26 days ago

https://github.com/matplotlib/mplfinance

A matplotlib utility for the visualization and visual analysis of financial data, designed to work seamlessly with Pandas DataFrames. It provides a new API for plotting OHLC data with support for candlestick, renko, pnf, and bar charts, as well as moving averages and volume panels. The library includes a Panels Method for simple subplots and an External Axes Mode for complex layouts and real-time animations. It also provides the original_flavor module for compatibility with the deprecated mpl-finance legacy API.

Tokens
4.7K
Snippets
3
Records
36
Agent score
89%

What's inside mplfinance

  1. Overview of mpl_finance

    master
    mpl_finance is a collection of functions designed for analyzing and plotting financial data using matplotlib. It provides specialized financial helpers that integrate with the matplotlib ecosystem. While originally part of the core Matplotlib codebase, it is now a standalone module that can leverage dependencies like pandas and scipy to provide a more powerful API for financial analysis.
  2. Run mplfinance animation examples

    master

    The animation examples are provided as Python scripts rather than Jupyter Notebooks to ensure proper display. To run them, clone the repository, navigate to the mplfinance/examples directory, and execute the desired script using Python.

    cd mplfinance/examples
    python mpf_animation_demo1.py
    python mpf_animation_demo2.py
    python mpf_animation_macd.py
    python mpf_animation_growingcandle.py
  3. Create subplots using the Panels Method

    master

    The Panels Method is a high-level way to create multiple plots (subplots) with minimal matplotlib knowledge. It is suitable for 95% of common financial plotting needs, such as combining OHLC/candlestick charts with volume and technical indicators (e.g., MACD, RSI, Bollinger Bands).

    Limitations:

    • Subplots are always stacked vertically.
    • All subplots share the same x-axis.
    • There is a maximum of 32 subplots.

    For a detailed implementation guide, refer to the Panels Method tutorial.

  4. Migrate from mpl-finance to mplfinance

    master
    The old mpl-finance package (installed as mpl-finance and imported as mpl_finance) is being deprecated in favor of mplfinance. The new package uses the same name for both installation and importing. Users should migrate to the mplfinance package to ensure continued support and access to the new API.
  5. Create subplots using the External Axes Method

    master

    The External Axes method provides full matplotlib flexibility by allowing you to create your own Figure and Axes objects and pass them into mplfinance. This is ideal for complex layouts, side-by-side plots, or real-time animations.

    Key Requirements & Responsibilities:

    • You are responsible for configuring the figure size, geometry, and axes locations.
    • You must call mplfinance.show() (or pyplot.show()) to display the figure.
    • When passing external axes, some mplfinance features may behave differently or be unavailable.

    Workflow:

    1. Create a figure using mpf.figure() (which supports the style= kwarg).
    2. Create axes using standard matplotlib methods like fig.add_subplot(), fig.add_axes(), or fig.subplots().
    3. Pass the axes into mpf.plot() using the ax= keyword argument.

    For a detailed implementation guide, refer to the External Axes notebook.

  6. Apply specific plot customizations via keyword arguments

    master

    For fine-grained control over individual plots, use keyword arguments (kwargs) within the plotting functions. This is useful for customizing:

    • Figure size and aspect ratio
    • Display of non-trading periods (e.g., weekends, holidays, after-hours trading)
    • Figure and Y-axis titles
    • X-axis datetime formatting and label rotation
    • Line colors for line plots
    • tight_layout settings
    • fill_between parameters
  7. Plot intraday data

    master
    For intraday data (e.g., 1-minute intervals), mplfinance automatically adjusts the x-axis to display TIME. If the data spans multiple trading days, the x-axis will automatically display both TIME and DATE. Moving average (mav) values will be calculated based on the number of data points (e.g., minutes) rather than days.
  8. Plot OHLC data with mpf.plot()

    master

    To plot financial data, start with a Pandas DataFrame containing OHLC (Open, High, Low, Close) data. The simplest way to generate a plot is to call mpf.plot(df) on your DataFrame. By default, the plot type is 'ohlc'.

    import mplfinance as mpf
    mpf.plot(daily)
  9. Use the New mplfinance API

    master

    The new API is designed to interface easily with Pandas DataFrames and automates much of the manual matplotlib configuration required by the old API.

    To use it, import the package as mpf and pass a Pandas DataFrame containing Open, High, Low, and Close data with a DatetimeIndex to the mpf.plot() function.

  10. Enable animation for real-time updates in mplfinance

    master
    To perform real-time updates to mplfinance plots, you must use the External Axes Mode. This mode allows you to create and manage your own Matplotlib Figure and Axes (SubPlots) and pass those Axes objects into mplfinance functions. Using External Axes Mode provides access to standard Matplotlib animation features.