据我所知,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
以与ax2
和ax3
对齐
它看起来不错,但我猜它使用了不同的日期格式,并且它与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在使用the panels method个子批时,实际上最多支持32个面板。(尽管documentation说它被限制为10个面板,但是you can see here版本为v0.12.7a17时,面板的最大数量从10个增加到32个。
pip install upgrade mplfinance
以获得最新版本)请阅读above mentioned documentation on the panels method,之后,如果您还有问题,请随时在此处发布或打开另一个SO问题
顺便说一句,您在上面似乎正在使用的external axis method,对于您试图完成的任务来说是不需要的。通常,不鼓励使用外部轴(绝对需要时除外),因为它会阻止某些功能(如果需要,需要您自己编写这些功能)。相反,如果您需要access to the mplfinance Figure and Axes对象,我建议您使用
returnfig=True
方法,该方法可以提供该访问,同时保留完整的mplfinance功能我希望这个答案有帮助。充分披露:我是mplfinance库的维护者
例如,使用上面的代码/数据:
结果是:
如the documentation所述,还可以调整绘图的许多特性(蜡烛和音量条的宽度和颜色、面板大小等)
相关问题 更多 >
编程相关推荐