mplfinance 中的独立面​​板

时间:2021-07-30 03:03:29

标签: python matplotlib mplfinance

我想分开我的面板,因为标题是重叠的。 ADX 标题在 RSI 面板中。我试过 tight_layout=True 但还是一样。

我的代码:

        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

1 个答案:

答案 0 :(得分:3)

Mplfinance 子图没有调整图形间距的功能,因此有一种方法可以将它们放入 matplotlib 子图中并使它们间隔开。我们建议使用 y 轴标签而不是标题,这是一项仅在 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

另一种解决方案

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 解决方案

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

相关问题