简体   繁体   English

面板重叠且 mplfinance 中的比率错误 plot

[英]Panel is overlapping and has a wrong ratio in mplfinance plot

I'm trying to plot a subplot but there are two problems.我正在尝试 plot 一个子图,但有两个问题。
#1 The panel_ratio setting (6,1) is unnoticed. #1 panel_ratio设置(6,1)未被注意到。
#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 #2 顶部面板的 y 轴向下突出并与底部面板的 y 轴重叠,以便在顶部面板中修剪条形图

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()

在此处输入图像描述

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 here .有关面板的详细参考,请参见此处

# 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', ylim=(10,90), panel=1)
mpf.plot(df[['open','high', 'low','close']], addplot=ap0, ylim=(2999,3104), panel_ratios=(6,1), style='charles')

mpf.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM