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

问题描述

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 相同的索引列。

结果:

X axis not aligned between axes 1,2,and 3

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

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

enter image description here

看起来不错,但我猜它使用了不同的日期格式,并且与 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,12.14,12.96],'Volume': [108,102,105,116,164,145,132,117],'cash': [100.0,100.295,95.685,95.635,95.635]
     })

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

f = mpf.figure()
(ax1,1]})   # add sharex=True as a param
f.subplots_adjust(hspace=0,wspace=0)

mpf.plot(ohlcv,volume=ax3)

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

mpf.show()

编辑:

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

解决方法

mplfinance 在使用 the panels method 子图时实际上最多支持 32 个面板。 (尽管 documentation 表示它仅限于 10 个面板,因为 you can see hereversion v0.12.7a17 中,面板的最大数量从 10 个增加到 32 个。{{ 1}} 获取最新版本)。

请阅读above mentioned documentation on the panels method之后,如果您仍有问题,请随时在此处发布或打开另一个 SO 问题。


顺便说一下,您似乎在上面使用的 external axis method 并不是您要完成的任务所必需的。通常,不鼓励使用外轴(除非绝对需要),因为它会阻止某些 mplfinance 功能(如果需要,需要您自己编写这些功能)。相反,如果您需要 access to the mplfinance Figure and Axes 对象,我会鼓励您使用 pip install --upgrade mplfinance 方法,它可以提供访问,同时保留完整的 mplfinance 功能。

我希望这个答案有帮助。完全公开:我是 mplfinance 库的维护者。


例如,使用上面的代码/数据:

returnfig=True

结果是:

enter image description here

还可以按照 the documentation 中的说明调整绘图的许多特征(蜡烛和音量条的宽度和颜色、面板大小等)。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...