如何将垂直移动悬停线添加到我的绘图图表

How do I add vertical moving hover line to my plotly chart

我正在努力实现这里所做的:https://www.quantalys.com/Fonds/120955 with javascript in python plotly。我想在 x 轴上添加悬停垂直线和红色注释。我在 goolgle 上做了一些搜索,但找不到我要找的答案。我当前的图表如下所示:

trace1 = go.Scatter(
x = df1.x,
y = df1.y,
name = "M&G OPTIMAL INCOME FD EUR AH ACC",
hoverinfo= 'name',
opacity=0.7,
mode = 'lines',
    line = dict(
    color = ('rgb(2, 12, 245)'),
    width = 1,
    ), 
)
trace2 = go.Scatter(
x = df2.x,
y = df2.y,
opacity=0.7,
name = "Alloc Flexible Prudent Monde",
hoverinfo= 'name',
mode = 'lines',
    line = dict(
    color = ('rgb(67, 45, 24)'),
    width = 1,
    )
)
trace3 = go.Scatter(
x = df3.x,
y = df3.y,
name = "25% MSCI World + 75% ML Global",
hoverinfo= 'name',
mode = 'lines',
opacity=0.7,
line = dict(
    color = ('rgb(205, 12, 24)'),
    width = 1,
    )
)

layout = go.Layout(

xaxis=dict(        
    showline=True,
    showgrid=True,
    showticklabels=True,
    linecolor='rgb(204, 204, 204)',
    linewidth=2,
    mirror=True,
),
yaxis=dict(
            showline=True,
    showgrid=True,
    showticklabels=True,
    linecolor='rgb(204, 204, 204)',
    linewidth=2,
    mirror=True,
),
showlegend=True,

)

data= [trace1, trace2,trace3]
fig = dict(data=data, layout=layout)
iplot(fig, filename='line-mode')

将此添加到您的布局定义中。

showlegend = True,
hovermode  = 'x'

将此添加到您的 xaxis 定义中。

showspikes = True,
spikemode  = 'across',
spikesnap = 'cursor',
showline=True,
showgrid=True,
...

并将其添加到您的布局定义中:

spikedistance =  -1,
xaxis=dict(...  

请参考这篇post and the documentation by plotly。 :)

编辑

您要求提供 x 轴标签。请使用

spikemode  = 'across+toaxis'

此外,我建议使用

spikedash = 'solid'

因为它更适合你的例子。