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

时间:2019-04-02 07:17:12

标签: python-3.x plotly onhover

我正在尝试实现以下操作:https://www.quantalys.com/Fonds/120955使用python中的javascript进行了密谋。我想在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')

1 个答案:

答案 0 :(得分:1)

将此添加到布局定义中。

import turtle

def goto_relative(dx, dy=None):
    """Moves the automatic global turtle by dx and dy (Or a given vector)"""
    goto_relative_on_turtle(turtle, dx, dy)

def goto_relative_on_turtle(t, dx, dy=None):
    """Moves al turtle by dx and dy (Or a given vector)"""
    if dy is None:
        dx, dy = dx
    t.goto(t.pos() + turtle.Vec2D(dx, dy))

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

showlegend = True,
hovermode  = 'x'

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

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

请参考postplotly的文档。 :)

编辑

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

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

另外,我建议使用

spikemode  = 'across+toaxis'

因为它更适合您的示例。