如何在我的Plotly图表上添加垂直移动的悬停线

9

我想在Python Plotly中实现这里所做的事情:https://www.quantalys.com/Fonds/120955,即添加悬停垂直线和红色x轴注释。我已经在谷歌上搜索了一些内容,但没有找到我要找的答案。我的当前图表如下:

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')
1个回答

16
将此添加到您的布局定义中。
showlegend = True,
hovermode  = 'x'

将此添加到您的 x 轴定义中。

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

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

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

请参考此帖子plotly文档。 :)

编辑

您要求x轴标签,请使用

spikemode  = 'across+toaxis'
此外,我建议使用 <\p>。
spikedash = 'solid'

因为它更适合您的例子。


1
我知道这不是你源代码的完美副本,但已经非常接近了。也许你可以自己尝试一下。 :) - Mike_H
1
谢谢回答。同时添加 spikedash = 'solid' - Stephinext
太棒了,很高兴我能帮到你! - Mike_H
你具体是什么意思? - Mike_H
如果你查看链接,你会看到日期的红色注释随着垂直线移动。 - Stephinext
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接