Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 611
Furqan Hashim Avatar asked Feb 21 '17 16:02

Furqan Hashim


People also ask

Do you have to import Matplotlib into Python?

Matplotlib is a Python library that helps to plot graphs. It is used in data visualization and graphical plotting. To use matplotlib, we need to install it.

Why do we use import Matplotlib Pyplot as PLT?

import matplotlib. pyplot as plt is shorter but no less clear. import matplotlib. pyplot as plt gives an unfamiliar reader a hint that pyplot is a module, rather than a function which could be incorrectly assumed from the first form.


2 Answers

In 2020, one can now pip install mplfinance

like image 157
duhaime Avatar answered Sep 20 '22 09:09

duhaime


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 106 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!

like image 37
Jonas Byström Avatar answered Sep 19 '22 09:09

Jonas Byström