I'm trying to plot a subplot but there are two problems.
#1 The panel_ratio
setting (6,1)
is unnoticed.
#2 The y axis of the top panel juts down and overlaps the y axis of the bottom panel, so that the bars are trimmed in the top panel
What is wrong with the code?
import pandas as pd
import numpy as np
from matplotlib.animation import FuncAnimation
import mplfinance as mpf
times = pd.date_range(start='2022-01-01', periods=50, freq='ms')
def get_rsi(df, rsi_period):
chg = df['close'].diff(1)
gain = chg.mask(chg<0,0)
loss = chg.mask(chg>0,0)
avg_gain = gain.ewm(com=rsi_period-1, min_periods=rsi_period).mean()
avg_loss = loss.ewm(com=rsi_period-1, min_periods=rsi_period).mean()
rs = abs(avg_gain/avg_loss)
rsi = 100 - (100/(1 rs))
return rsi
df = pd.DataFrame(np.random.randint(3000, 3100, (50, 1)), columns=['open'])
df['high'] = df.open 5
df['low'] = df.open-2
df['close'] = df.open
df['rsi14'] = get_rsi(df, 14)
df.set_index(times, inplace=True)
lows_peaks = df.low.nsmallest(5).index
fig = mpf.figure(style="charles",figsize=(7,8))
ax1 = fig.add_subplot(1,1,1)
ax2 = fig.add_subplot(2,1,2)
ap0 = [ mpf.make_addplot(df['rsi14'],color='g', ax=ax2, ylim=(10,90), panel=1) ]
mpf.plot(df, ax=ax1, ylim=(2999,3104), addplot=ap0, panel_ratios=(6,1))
mpf.show()
CodePudding user response:
In this case, it is easier to use a panel instead of an external axis. I tried your code and could not improve it. For a detailed reference on panels, see