Line hover text in Plotly

霸气de小男生 提交于 2019-12-04 07:50:44

One quick solution/hack is to follow etienne's idea from the community forum page that Maximilian mentioned in the comment.
The idea is to insert a transparent node on each line and annotate it with a hover text label.

here is a code that worked for me

trace3_list = []
middle_node_trace = go.Scatter(
    x=[],
    y=[],
    text=[],
    mode='markers',
    hoverinfo='text',
    marker=go.Marker(
        opacity=0
    )
)
for edge in G.edges(data=True):
    trace3=Scatter(
        x=[],
        y=[],
        mode='lines',
        line=Line(color='rgb(210,210,210)', width=edge[2]['weight']),
        hoverinfo='none'
    )
    x0, y0 = G.node[edge[0]]['pos']
    x1, y1 = G.node[edge[1]]['pos']
    trace3['x'] += [x0, x1, None]
    trace3['y'] += [y0, y1, None]
    trace3_list.append(trace3)

    middle_node_trace['x'].append((x0+x1)/2)
    middle_node_trace['y'].append((y0+y1)/2)
    middle_node_trace['text'].append(str(edge[2]['weight']))

Then just plot all the traces, i.e. [*trace3_list, middle_node_trace].

BONUS: having a separate trace for each edge allows to set different widths, e.g. proportional to the edge weight.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!