How to import candlestick_ohlc and candlestick2_ohlc in Matplolib.finance

Viewed 1373

I have some-what old code where I was using a method called candlestick2_ohlc from matplotlib package. And the code goes something like that:

import matplotlib.pyplot as plt
from matplotlib.finance import candlestick2_ohlc

...
fig = plt.figure(facecolor='#131722',dpi=135)
ax1 = plt.subplot2grid((6,4), (1,0), rowspan=4, colspan=4, facecolor='#131722')
candlestick2_ohlc(ax1, ...)
...

And now it shows this error message:

ModuleNotFoundError: No module named 'matplotlib.finance'

I'm using python 3.7.6 and matplotlib 3.1.3

1 Answers

After some time, I've found out that matplotlib.finance was deprecated since version 2.0 according to the official documentation here. And the matplotlib people have created a standalone package called mplfinance that will replace a former package called mpl_finance by mid-2020.

So, starting from now. In order to use candlestick2_ohlc, you need to install mplfinance using pip:

pip install --upgrade mplfinance

Then, you can import candlestick2_ohlc or candlestick_ohlc like so:

from mplfinance.original_flavor import candlestick_ohlc
Related