Furqan Hashim
Furqan Hashim

Reputation: 1318

Since matplotlib.finance has been deprecated, how can I use the new mpl_finance module?

I am trying to import matplotlib.finance module in python so that I can make a Candlestick OCHL graph. My matplotlib.pyplot version is 2.00. I've tried to import it using the following commands:

import matplotlib.finance
from matplotlib.finance import candlestick_ohlc

I get this error:

warnings.warn(message, mplDeprecation, stacklevel=1) MatplotlibDeprecationWarning: The finance module has been deprecated in mpl 2.0 and will be removed in mpl 2.2. Please use the module mpl_finance instead.

Then instead of using the above lines in python I tried using the following line:

import mpl_finance

I get this error:

ImportError: No module named 'mpl_finance'

What should I do to import candlestick from matplotlib.pyplot?

Upvotes: 46

Views: 117667

Answers (10)

Jonas Byström
Jonas Byström

Reputation: 26169

I've stopped using mpl_finance (and plotly) since they are too slow. Instead I've written an optimized finance plotting library, finplot, which I use to backtest up to 107 candles.

Here's a small example:

import yfinance as yf
import finplot as fplt

df = yf.download('SPY',start='2018-01-01', end = '2020-04-29')
fplt.candlestick_ochl(df[['Open','Close','High','Low']])
fplt.plot(df.Close.rolling(50).mean())
fplt.plot(df.Close.rolling(200).mean())
fplt.show()

Examples included show SMA, EMA, Bollinger bands, Accumulation/Distribution, Heikin Ashi, on balance volume, RSI, TD sequential, MACD, scatter plot indicators, heat maps, histograms, real-time updating charts and interactive measurements; all with sensible defaults ready for use.

MACD S&P 500 example

I do dogfooding every day, drop me a note or a pull request if there is something you want. Hope you take it for a spin!

Upvotes: 52

CASEDEARTISTA
CASEDEARTISTA

Reputation: 1

Replace from matplotlib.finance import candlestick_ohlc with from mplfinance.original_flavor import candlestick_ohlc , That should work.

Upvotes: 0

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7714

There is a new version of matplotlib finance, with documentation, here:

Install with:   pip install --upgrade mplfinance

Or with:   conda install -c conda-forge mplfinance

NOTE: The package name no longer has the dash or underscore:
It is now mplfinance (not mpl-finance, nor mpl_finance)

Upvotes: 12

duhaime
duhaime

Reputation: 27594

In 2020, one can now pip install mplfinance

Upvotes: 44

Quantum Prophet
Quantum Prophet

Reputation: 455

Simply use pip install mpl_finance for Windows or pip3 install mpl_finance for Linux/Unix for installation.

Then use from mpl_finance import candlestick_ohlc to call the library in the Jupyter notebook!

Upvotes: 0

Priyansh gupta
Priyansh gupta

Reputation: 916

I'm working on google colab , i got the same problem . then what i did -for python3.6

import mpl_finance

from mpl_finance import candlestick_ohlc

Upvotes: 6

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339350

What this warning tells you is that the finance module will be removed at some point.

At the moment you don't need to worry about this warning. It will only affect you when you update to a yet to be released version 2.2 of matplotlib, in which case you'll need to change your imports.

If you already want to be compatible with future versions now, you can download the mpl_finance module from https://github.com/matplotlib/mpl_finance .

After having downloaded the files, you may install in the usual way,

python setup.py install

Alternatively you may try installing through pip,

pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

The reason for this is that the people at matplotlib want to keep their code clean and not maintain a specialized sidepackage like this in the main code. They probably also do not want to maintain the package and spend resources on it, which can be better used in the core development.

Upvotes: 33

Ben2209
Ben2209

Reputation: 1121

Plotly.py, a web-browser based, interactive plotting module has finance plotting functions https://plot.ly/python/candlestick-charts/. And it is maintained.

Upvotes: 1

magraf
magraf

Reputation: 460

mpl_finance is no longer part of matplotlib. Install the module directly from gitHub via pip

pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

and import it with

from mpl_finance import candlestick_ohlc

Then it works the same as before.

Upvotes: 13

Kattern
Kattern

Reputation: 3109

Since mpl_finace is not on pip now, you may also want to use following command to install mpl_finance by pip:

pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

Upvotes: 26

Related Questions