mplfinance绘图(带外部轴模式),附加绘图(带日期格式问题)

2024-09-28 18:57:44 发布

您现在位置:Python中文网/ 问答频道 /正文

据我所知,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, 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()

编辑:

现在我仔细看了看,音量条也太大了,在蜡烛下面的视觉上没有正确对齐。轴之间的网格线也非常混乱


Tags: true绘图indexplotascashpdohlcv
1条回答
网友
1楼 · 发布于 2024-09-28 18:57:44

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库的维护者


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

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

结果是:

enter image description here

the documentation所述,还可以调整绘图的许多特性(蜡烛和音量条的宽度和颜色、面板大小等)

相关问题 更多 >