3

I am trying to plot 5 charts one under the other with mplfinance.

This works:

for coin in coins:
    mpf.plot(df_coins[coin], title=coin, type='line', volume=True, show_nontrading=True)

However each plot is a separate image in my Python Notebook cell output. And the x-axis labelling is repeated for each image.

I try to make a single figure containing multiple subplot/axis, and plot one chart into each axis:

from matplotlib import pyplot as plt

N = len(df_coins)
fig, axes = plt.subplots(N, figsize=(20, 5*N), sharex=True)

for i, ((coin, df), ax) in zip(enumerate(df_coins.items()), axes):
    mpf.plot(df, ax=ax, title=coin, type='line', volume=True, show_nontrading=True)

This displays subfigures of the correct dimensions, however they are not getting populated with data. Axes are labelled from 0.0 to 1.0 and the title is not appearing.

What am I missing?

P i
  • 29,020
  • 36
  • 159
  • 267

2 Answers2

3

There are two ways to subplot. One is to set up a figure with mplfinance objects. The other way is to use your adopted matplotlib subplot to place it.

yfinace data

import matplotlib.pyplot as plt
import mplfinance as mpf
import yfinance as yf

tickers = ['AAPL','GOOG','TSLA']
data = yf.download(tickers, start="2021-01-01", end="2021-03-01", group_by='ticker')

aapl = data[('AAPL',)]
goog = data[('GOOG',)]
tsla = data[('TSLA',)]

mplfinance

fig = mpf.figure(style='yahoo', figsize=(12,9))
#fig.subplots_adjust(hspace=0.3)
ax1 = fig.add_subplot(3,1,1, sharex=ax3)
ax2 = fig.add_subplot(3,1,2, sharex=ax3)
ax3 = fig.add_subplot(3,1,3)

mpf.plot(aapl, type='line', ax=ax1, axtitle='AAPL', xrotation=0)
mpf.plot(goog, type='line', ax=ax2, axtitle='GOOG', xrotation=0)
mpf.plot(tsla, type='line', ax=ax3, axtitle='TSLA', xrotation=0)
ax1.set_xticklabels([])
ax2.set_xticklabels([])

matplotlib

N = len(tickers)
fig, axes = plt.subplots(N, figsize=(20, 5*N), sharex=True)

for df,t,ax in zip([aapl,goog,tsla], tickers, axes):
    mpf.plot(df, ax=ax, axtitle=t, type='line', show_nontrading=True)# volume=True 

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • 1
    The volume subplot is not supported, so it is removed from the settings. Please refer to the example in the [official reference](https://github.com/matplotlib/mplfinance/blob/master/examples/external_axes.ipynb). I don't know the name of the virtual currency, so I used the stock data. – r-beginners Oct 30 '21 at 08:04
  • 2
    It's ***not*** that the volume subplot is not support. It **is** that when you are in ***external axes mode*** then mplfinance does *not* know where to put the volume ***unless you tell it***. Therefore instead of `volume=True` ***you must write*** **`volume=vaxes`** where `vaxes` is the axes that you created where you want the volume to be! – Daniel Goldfarb Oct 30 '21 at 23:23
2

In addition to the techniques mentioned by @r-beginners there is another technique that may work for you in the case where all plots share the same x-axis. That is to use mpf.make_addplot().

aps = []
for coin in coins[1:]:
    aps.append(mpf.make_addplot(df_coins[coin]['Close'], title=coin, type='line'))

coin = coins[0]
mpf.plot(df_coins[coin],axtitle=coin,type='line',volume=True,show_nontrading=True,addplot=aps)

If you choose to do type='candle' instead of 'line', then change

df_coins[coin]['Close']

to simply

df_coins[coin]
Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
  • My first attempt was to use `make_add_plot()`, but I couldn't do it with an error that a data frame was required. I see that the base graph needs a data frame containing the ohlc values. I have learned. – r-beginners Oct 31 '21 at 03:33
  • @r-beginners yes, `mpf.plot()` always requires an OHLC DataFrame (or OHLCV). Whereas `mpf.make_addplot()` accepts a list, array, or series. The one exception is *if, in the call to* `make_addplot()`, `type='candle'` or `type='ohlc'`, then `make_addplot()` will also demand an OHLC DataFrame. – Daniel Goldfarb Oct 31 '21 at 11:20
  • Thank you for the detailed explanation. I understand mpf better again. – r-beginners Oct 31 '21 at 11:26