7

I would like to separate my panels because the titles superposed. ADX title is in RSI panel. I tried with tight_layout=True but still the same.

My code:

        ap0 = [
        mpf.make_addplot(df['sma_200'],color='#FF0000', panel=2),
        mpf.make_addplot(df['sma_50'],color='#ffa500', panel=2),
        mpf.make_addplot(df['sma_20'],color='#00FF00', panel=2),
        mpf.make_addplot(df['rsi'],color='#ffa500', panel=0, title="RSI"),
        mpf.make_addplot(df['hline_30'], panel=0),
        mpf.make_addplot(df['hline_70'], panel=0),
        mpf.make_addplot(df['adx'],color='#0000FF', panel=1, secondary_y=False, title="ADX"),
        mpf.make_addplot(df['-di'],color='#FF0000', panel=1, secondary_y=False),
        mpf.make_addplot(df['+di'],color='#32cd32', panel=1, secondary_y=False),
        mpf.make_addplot(df['hline_25'], panel=1, secondary_y=False)
    ]
    fig, axlist = mpf.plot(
        df,
        panel_ratios=(.05, .05, .2, .05),
        type="hollow_candle",
        yscale='log',
        volume=True,
        title="{} - {}".format(ticker, interval),
        style=mpf_style,
        figsize=(12.8, 10),
        returnfig=True,
        closefig=True,
        addplot=ap0,
        main_panel=2,
        volume_panel=3,
        num_panels=4,
    )

enter image description here

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
Martin Bouhier
  • 212
  • 2
  • 12
  • 1
    In addition to the two suggestions in r-beginners answer below (which are very good btw), you can also try a combination of adjusting the [**panel ratios**](https://github.com/matplotlib/mplfinance/blob/master/examples/panels.ipynb) to make the upper panels somewhat bigger, and setting `ylim` in the `make_addplot()` calls so that the data itself takes up less space in those panels. This will effectively create more space in those panels leaving more room for the titles. I'm not 100% sure this will work (haven't tried yet) but it might. – Daniel Goldfarb Jul 30 '21 at 11:23
  • Another idea, which I haven't tried yet but I think may work: When creating your custom `mpf_style` from `mpf.make_mpf_style()` include the kwarg `rc={"axes.titley":0.8}` ... not necessarily 0.8 but (I think) any number below 1.0 which represents the very top of the panel. This parameter (`"axes.titley"`) will raise or lower the position of `title` passed into `make_addplot()`. By playing around with its value you may be able to find a position which is just right for your plot. – Daniel Goldfarb Jul 30 '21 at 12:23

1 Answers1

10

Mplfinance subplots do not have a function to adjust the graph spacing, so there is a way to fit them into matplotlib subplots and make them spaced. We recommend using the y-axis label instead of the title, which is a feature only available in Mplfinance.

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

df = yf.download("AAPL", start="2021-01-01", end="2021-07-01")
df['sam_20'] = df['Close'].rolling(20).mean()
df['sam_50'] = df['Close'].rolling(50).mean()
fig = mpf.figure(style='yahoo',figsize=(7,8))
ax1 = fig.add_subplot(4,1,1, sharex=ax4)
ax2 = fig.add_subplot(4,1,2, sharex=ax4)
ax3 = fig.add_subplot(4,1,3, sharex=ax4)
ax4 = fig.add_subplot(4,1,4)

ap0 = [
    mpf.make_addplot(df['sam_20'], color='#00FF00', panel=0, title='sma_20', ax=ax1),
    mpf.make_addplot(df['sam_50'], color='#FFa500', panel=1, title='sma_50', ax=ax2)
]

mpf.plot(df,ax=ax3, addplot=ap0, volume=ax4, )
fig.subplots_adjust(hspace=0.2)
ax1.tick_params(labelbottom=False)
ax2.tick_params(labelbottom=False)
ax3.tick_params(labelbottom=False)
ax3.yaxis.set_label_position('left')
ax3.yaxis.tick_left()

enter image description here

Another solution

ap0 = [
    mpf.make_addplot(df['sam_20'], color='#00FF00', panel=0, ylabel='sma_20'),
    mpf.make_addplot(df['sam_50'], color='#FFa500', panel=1, ylabel='sma_50')
]

enter image description here

@Daniel Goldfarlb solution

ap0 = [
    mpf.make_addplot(df['sam_20'], color='#00FF00', panel=0, title='sma_20', ylim=(110,140)),
    mpf.make_addplot(df['sam_50'], color='#FFa500', panel=1, title='sma_50')
]

mpf.plot(
        df,
        panel_ratios=(2, 1, 3, 1),
        type="hollow_candle",
        yscale='log',
        volume=True,
        style='yahoo',
        figsize=(12.8, 10),
        addplot=ap0,
        main_panel=2,
        volume_panel=3,
        num_panels=4,
    )

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • 1
    @Daniel Goldfarb I have actually added your comment approach. I see this approach as an option. – r-beginners Jul 30 '21 at 12:28
  • Is there any way to add customs size for the axs? Because I need the indicators size is smaller than the candles. I'm working with the r-beginners solution – Martin Bouhier Aug 02 '21 at 22:20
  • 1
    If you want to change the size of each of them, you can do so by specifying `height_ratios=[0.25,0.25,0.5,0.25]` in GridSpec. See [this](https://matplotlib.org/stable/api/_as_gen/matplotlib.gridspec.GridSpec.html#matplotlib.gridspec.GridSpec). – r-beginners Aug 03 '21 at 03:12