带有外轴模式的 mplfinance 图,带有日期格式问题的附加图

mplfinance plot with external axis mode, additional plot with date format issue

据我所知,mplfinance 库只支持 2 个图,称为面板 0 和 1。我想要 3 个面板,所以事实上,我正在使用外轴,自己创建轴,比如这个:

f = mpf.figure()
(ax1, ax2, ax3) = f.subplots(3, 1, gridspec_kw={'height_ratios': [1, 3, 1]}, sharex=True)
f.subplots_adjust(hspace=0, wspace=0)

# mpf.plot requires to create an index column for dates
ohlcv = ohlcv.set_index(column_names[0])

mpf.plot(ohlcv, type='candle', ax=ax2, volume=ax3)

无法将 ax1 作为参数添加到 mpf.plot 函数(或者是吗?),所以我尝试了这个:

ax1.plot(ohlcv.index.values, self._ohlcv['cash'])

我认为它会起作用,因为它使用与 mpf.plot.

相同的索引列

结果:

所以我不知道应该如何绘制 ax1 以与 ax2ax3 对齐。

如果我不使用 sharex=True 情节如下所示:

它看起来不错,但我猜它使用了不同的日期格式并且它与 ax2 和 ax3 不完全一致。那么我怎样才能使它起作用呢?

示例代码:

import pandas as pd
import mplfinance as mpf

ohlcv = pd.DataFrame(
    {'Date': [1609459200, 1609545600, 1609632000, 1609718400, 1609804800, 1609891200, 1609977600, 1610064000],
     'Open': [11.25, 12.61, 11.93, 10.52, 10.41, 11.66, 11.47, 12.14],
     'High': [12.63, 13.2, 11.94, 12.12, 15.02, 11.71, 12.47, 13.01],
     'Low': [11.10, 11.68, 9.93, 10.3, 10.31, 11.26, 10.46, 12.13],
     'Close': [12.61, 11.93, 10.52, 10.41, 11.66, 11.47, 12.14, 12.96],
     'Volume': [108, 102, 105, 116, 164, 145, 132, 117],
     'cash': [100.0, 100.295, 100.295, 100.295, 95.685, 95.635, 95.635, 95.635]
     })

ohlcv.iloc[:, 0] = pd.to_datetime(ohlcv.iloc[:, 0], unit='s')
ohlcv = ohlcv.set_index('Date')

f = mpf.figure()
(ax1, ax2, ax3) = f.subplots(3, 1, gridspec_kw={'height_ratios': [1, 3, 1]})   # add sharex=True as a param
f.subplots_adjust(hspace=0, wspace=0)

mpf.plot(ohlcv, type='candle', ax=ax2, volume=ax3)

ax1.plot(ohlcv.index.values, ohlcv['cash'])
ax1.legend(['Cash'], loc='best')

mpf.show()

编辑:

现在我仔细观察一下,成交量条也过大并且在视觉上没有正确对齐到蜡烛图下方。轴之间的网格线也很混乱。

mplfinance 实际上最多支持 32 个面板 当使用 the panels method of subplots. (Although the documentation says that it is limited to 10 panels, as you can see hereversion v0.12.7a17 最大面板数时已从 10 增加到 32。pip install --upgrade mplfinance 获取最新版本)。

请阅读 above mentioned documentation on the panels method, 之后,如果您仍有问题,请随时 post 在这里提出或提出另一个 SO 问题。


顺便说一句,external axis method, which you appear to be using above, is not needed for what you are trying to accomplish. Generally, using external axes is discouraged (except when absolutlely needed) because it blocks some mplfinance features (requiring you to code those features yourself if you want them). Rather, if you require access to the mplfinance Figure and Axes 对象,我鼓励你使用 returnfig=True 方法它可以提供访问权限,同时保留完整的 mplfinance 功能。

希望这个回答对您有所帮助。全面披露:我是 mplfinance 库的维护者。


例如,使用上面的 code/data:

import pandas as pd
import mplfinance as mpf
ohlcv = pd.DataFrame(
    {'Date': [1609459200, 1609545600, 1609632000, 1609718400,
              1609804800, 1609891200, 1609977600, 1610064000],
     'Open': [11.25, 12.61, 11.93, 10.52, 10.41, 11.66, 11.47, 12.14],
     'High': [12.63, 13.2, 11.94, 12.12, 15.02, 11.71, 12.47, 13.01],
     'Low': [11.10, 11.68, 9.93, 10.3, 10.31, 11.26, 10.46, 12.13],
     'Close': [12.61, 11.93, 10.52, 10.41, 11.66, 11.47, 12.14, 12.96],
     'Volume': [108, 102, 105, 116, 164, 145, 132, 117],
     'cash': [100.0, 100.295, 100.295, 100.295, 95.685, 95.635, 95.635, 95.635]
     })

ohlcv.iloc[:, 0] = pd.to_datetime(ohlcv.iloc[:, 0], unit='s')
ohlcv = ohlcv.set_index('Date')

ap = mpf.make_addplot(ohlcv['cash'],panel=0,ylabel='Cash')
mpf.plot(ohlcv,
         type='candle',
         volume=True,
         main_panel=1,
         volume_panel=2,
         addplot=ap,
         figsize=(7,7))

结果是:

还可以按照 the documentation.

中所述调整绘图的许多特征(蜡烛和音量条的宽度和颜色、面板大小等)