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,
)
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()
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')
]
@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,
)
@Daniel Goldfarb I have actually added your comment approach. I see this approach as an option.
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
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.