matplotlib utilities for the visualization, and visual analysis, of financial data
pip install --upgrade mplfinance
- The New API
- Basic Usage
- Adding Your Own Technical Studies to Plots
- Saving the Plot to a File
- Customizing the Appearance of Plots
- Renko Plots
- Technical Studies (presently in development)
- Latest Release Info
- Some Background History About This Package
- Old API Availability
matplotlib/mplfinance, contains a new matplotlib finance API that makes it easier to create financial plots. It interfaces nicely with Pandas DataFrames.
More importantly, the new API automatically does the extra matplotlib work that the user previously had to do "manually" with the old API. (The old API is still available within this package; see below).
The conventional way to import the new API is as follows:
import mplfinance as mpf
The most common usage is then to call
data is a
Pandas DataFrame object containing Open, High, Low and Close data, with a Pandas
I am very interested to hear from you regarding what you think of the new
mplfinance, plus any suggestions you may have for improvement. You can reach me at email@example.com or, if you prefer, provide feedback or a ask question on our issues page.
Start with a Pandas DataFrame containing OHLC data. For example,
import pandas as pd daily = pd.read_csv('examples/data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True) daily.index.name = 'Date' daily.shape daily.head(3) daily.tail(3)
After importing mplfinance, plotting OHLC data is as simple as calling
mpf.plot() on the dataframe
import mplfinance as mpf mpf.plot(daily)
The default plot type, as you can see above, is
'ohlc'. Other plot types can be specified with the keyword argument
type, for example,
We can also plot moving averages with the
- use a scalar for a single moving average
- use a tuple or list of integers for multiple moving averages
We can also display
Notice, in the above chart, there are no gaps along the x-coordinate, even though there are days on which there was no trading. Non-trading days are simply not shown (since there are no prices for those days).
However, sometimes people like to see these gaps, so that they can tell, with a quick glance, where the weekends and holidays fall.
Non-trading days can be displayed with the
For example, in the chart below, you can easily see weekends, as well as a gap at Thursday, November 28th for the U.S. Thanksgiving holiday.
We can also plot intraday data:
intraday = pd.read_csv('examples/data/SP500_NOV2019_IDay.csv',index_col=0,parse_dates=True) intraday = intraday.drop('Volume',axis=1) # Volume is zero anyway for this intraday data set intraday.index.name = 'Date' intraday.shape intraday.head(3) intraday.tail(3)
The above dataframe contains Open,High,Low,Close data at 1 minute intervervals for the S&P 500 stock index for November 5, 6, 7 and 8, 2019. Let's look at the last hour of trading on November 6th, with a 7 minute and 12 minute moving average.
iday = intraday.loc['2019-11-06 15:00':'2019-11-06 16:00',:] mpf.plot(iday,type='candle',mav=(7,12))
The "time-interpretation" of the
mav integers depends on the frequency of the data, because the mav integers are the number of data points used in the Moving Average (not the number of days or minutes, etc). Notice above that for intraday data the x-axis automatically displays TIME instead of date. Below we see that if the intraday data spans into two (or more) trading days the x-axis automatically displays BOTH TIME and DATE
iday = intraday.loc['2019-11-05':'2019-11-06',:] mpf.plot(iday,type='candle')
In the plot below, we see what an intraday plot looks like when we display non-trading time periods with
show_nontrading=True for intraday data spanning into two or more days.
Below: 4 days of intraday data with
Below: the same 4 days of intraday data with
show_nontrading defaulted to
Below: Daily data spanning across a year boundary automatically adds the YEAR to the DATE format
df = pd.read_csv('examples/data/yahoofinance-SPY-20080101-20180101.csv',index_col=0,parse_dates=True) df.shape df.head(3) df.tail(3)
For more examples of using mplfinance, please see the jupyter notebooks in the
- implement save figure to in-memory buffer
-- Thank you Caleb Coffin @coffincw for contributing the above.
|0.12.3a2||- implement custom markers (issue#30)
- fix minor issue with chart
-- Thank you Amir Atashin @amiaty for contributing the above.
- add internal functions:
|0.12.3a1||- fix issue#28: math.log crash on zero in data
- remove "Implemented" field from kwarg dicts
- yahoo style show colors for
- customize aspect ratio, figure title, y-labels
- customize colors and other
- secondary y-axis available to
- bug fix for volume widths
|0.12.0a3||Increase mav limit from 3 to 7 different mavs||2020-01-16|
|0.12.0a2||Ability to save plot to a file (pdf, svg, png, jpg, ...)||2020-01-14|
|0.12.0a1||Ability to plot arbitrary user data (in addition to basic OHLCV data).
- both line and scatter plots available.
- optionally plot on either the "main" or "lower" (aka "volume") axis.
|0.11.x||Basic Plotting from Pandas DataFrame of OHLC bars and candlesticks.
- optional display of volume
- optional display of (up to 3 different) moving averages.
- old API still available by importing from "mplfinance/original_flavor"
|0.10.x||Old mpl-finance API set up as its own package
(i.e. removed from the matplotlib package).
My name is Daniel Goldfarb. In November 2019, I became the maintainer of
matplotlib/mpl-finance. That module is being deprecated in favor of the current
matplotlib/mplfinance. The old
mpl-finance consisted of code extracted from the deprecated
matplotlib.finance module along with a few examples of usage. It has been mostly un-maintained for the past three years.
It is my intention to archive the
matplotlib/mpl-finance repository soon, and direct everyone to
matplotlib/mplfinance. The main reason for the rename is to avoid confusion with the hyphen and the underscore: As it was,
mpl-finance was installed with the hyphen, but imported with an underscore
mpl_finance. Going forward it will be a simple matter of both installing and importing
With this new
mplfinance package installed, in addition to the new API, users can still access the old API (at least for the next several months) by changing their import statments
from mpl_finance import <method>
from mplfinance.original_flavor import <method>
<method> indicates the method you want to import, for example:
from mplfinance.original_flavor import candlestick_ohlc