mplfinance plot tight_layout 切断信息
mplfinance plot tight_layout cuts information off
首先必须说,我喜欢 mplfinance,它是在图表上显示数据的一种非常好的方式。
我现在的问题是,我无法将 space 缩小到边框。有一个参数 calle "tight_layout" 但它切断了信息。可能是我做错了。
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
时,它看起来像这样:
图表周围的space是完美的,但是图表中的数据被截断了。
如果我使用 tight_layout=False
它会使用太多 space 并且创建的 html 文件看起来不正确。
有人知道正确的方法吗?
您可以采取多种不同的措施来解决此问题。首先,了解发生这种情况的原因。 tight_layout
算法将 x 轴限制设置为刚好超出数据框日期时间索引的限制,而您的某些 alines
点显然超出了此范围。鉴于此,您可以做一些事情:
- 使用 kwarg
xlim=(xmin,xmax)
手动设置您想要的 x 轴范围。
- 用
nan
值填充您的 ohlc 数据框的末尾,直到您在绘图中需要的最新日期。
- 请求
tight_layout
应 alines
考虑的错误修复或增强功能。
HTH.
P.S. 目前 xlim
只接受与数据框中的行号(或与 matplotlib 日期对应)相对应的数字(整数或浮点数) , 请参见下面的 P.P.S.)。我希望尽快增强 xlim
以接受日期。与此同时,尝试这样的事情:
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 的用户不必担心这些日期转换细节。
首先必须说,我喜欢 mplfinance,它是在图表上显示数据的一种非常好的方式。
我现在的问题是,我无法将 space 缩小到边框。有一个参数 calle "tight_layout" 但它切断了信息。可能是我做错了。
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
时,它看起来像这样:
如果我使用 tight_layout=False
它会使用太多 space 并且创建的 html 文件看起来不正确。
有人知道正确的方法吗?
您可以采取多种不同的措施来解决此问题。首先,了解发生这种情况的原因。 tight_layout
算法将 x 轴限制设置为刚好超出数据框日期时间索引的限制,而您的某些 alines
点显然超出了此范围。鉴于此,您可以做一些事情:
- 使用 kwarg
xlim=(xmin,xmax)
手动设置您想要的 x 轴范围。 - 用
nan
值填充您的 ohlc 数据框的末尾,直到您在绘图中需要的最新日期。 - 请求
tight_layout
应alines
考虑的错误修复或增强功能。
HTH.
P.S. 目前 xlim
只接受与数据框中的行号(或与 matplotlib 日期对应)相对应的数字(整数或浮点数) , 请参见下面的 P.P.S.)。我希望尽快增强 xlim
以接受日期。与此同时,尝试这样的事情:
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 的用户不必担心这些日期转换细节。