mplfinance绘图紧凑的布局会切断信息

2024-09-24 00:24:51 发布

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

首先必须说,我喜欢mplfinance,它是在图表上显示数据的一种非常好的方式

我现在的问题是,我无法将空间缩小到边界。有一个称为“紧密布局”的参数,但它会切断信息。也许我做错了什么

mpf.plot(df_history, show_nontrading=True, figratio=(10,7), figscale=1.5, datetime_format='%d.%m.%y', 
         xrotation=90, tight_layout=True,  
         alines=dict(alines=seq_of_points, colors=seq_of_colors, linestyle='-', linewidths=0.5),
         type='candle', savefig=bildpfad, addplot=apdict, 
         update_width_config=dict(candle_linewidth=0.4))

当我使用tight_layout=True时,它看起来是这样的: enter image description here 图表周围的空间是完美的,但图表中的数据被切断

如果我使用tight_layout=False,它会占用太多的空间,并且创建的html文件看起来是扭曲的。 enter image description here

有人知道正确的方法吗


Tags: of数据true方式图表空间seqdict
1条回答
网友
1楼 · 发布于 2024-09-24 00:24:51

你可以做一些不同的事情来解决这个问题。首先,了解发生这种情况的原因。tight_layout算法将x轴限制设置为刚好超出数据帧日期时间索引的限制,而某些alines点显然超出了该范围。鉴于此,您可以做以下几件事:

  • 使用kwargxlim=(xmin,xmax)手动设置所需的x轴限制
  • nan值填充ohlc数据框的末尾,直到绘图上需要的最新日期
  • 请求tight_layout应该考虑的bug修复或增强


p.S.目前xlim只接受与数据帧中的行号相对应的数字(int或float)(或与matplotlib日期相对应的数字,请参见下面的p.p.S.)。我希望不久的某个时候能接受约会。同时,试着这样做:

xmin = 0
xmax = len(df_history)*1.4
mpf.plot(df_history,...,xlim=(xmin,xmax))

p.p.S.我刚刚意识到上述(xmax = len(df_history)*1.4)将只适用于show_nontrading=False。但是,如果使用show_nontrading=True,则需要以不同的方式设置xmax和xmin,如下所示:

import matplotlib.dates as mdates
...

# decide how many days past the end of the data 
# that you want the maximum x-axis limit to be:
numdays = 10
# then:
xmin = mdates.date2num(df_history.index[0].to_py_datetime())
xmax = mdates.date2num(df_history.index[-1].to_py_datetime()) + numdays
mpf.plot(df_history,...,xlim=(xmin,xmax))

(请注意,上面的两个函数都不是.index[0],而是xmax派生自.index[-1]


我很抱歉,上述针对xlim的解决方案过于详细。这更激励我完成xlim增强功能,这样用户就可以将日期作为字符串或日期时间传入。mplfinance的用户不必担心这些日期转换细节

相关问题 更多 >