Overview of mpl_finance
masterpandas and scipy to provide a more powerful API for financial analysis.repository·master·Indexed 26 days ago
https://github.com/matplotlib/mplfinanceA 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.
pandas and scipy to provide a more powerful API for financial analysis.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.pyThe 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:
For a detailed implementation guide, refer to the Panels Method tutorial.
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.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:
mplfinance.show() (or pyplot.show()) to display the figure.mplfinance features may behave differently or be unavailable.Workflow:
mpf.figure() (which supports the style= kwarg).matplotlib methods like fig.add_subplot(), fig.add_axes(), or fig.subplots().mpf.plot() using the ax= keyword argument.For a detailed implementation guide, refer to the External Axes notebook.
For fine-grained control over individual plots, use keyword arguments (kwargs) within the plotting functions. This is useful for customizing:
tight_layout settingsfill_between parametersmplfinance 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.For broad, consistent styling across multiple plots, use mplfinance styles. This allows you to define global settings for:
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)Install mplfinance using pip. Note that mplfinance requires both matplotlib and pandas to function.
pip install --upgrade mplfinanceThe 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.
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.