图形边缘标签

时间:2016-04-22 10:37:55

标签: python graph plotly

我想在plotly中向图表添加标签。我写了一个像这样的程序,但它不起作用

edge_trace = Scatter(
    x=[],
    y=[],
    line=Line(width=3, color='rgba(136, 136, 136, .8)', text=[]),
    hoverinfo='none',
    text='',
    mode='lines')

for edge in G.edges():
    x0, y0 = pos[edge[0]]
    x1, y1 = pos[edge[1]]
    edge_trace['x'] += [x0, x1, None]
    edge_trace['y'] += [y0, y1, None]
    edge_trace['line']['text'].append('label')

我收到以下错误:

    raise exceptions.PlotlyDictKeyError(self, path)
    plotly.exceptions.PlotlyDictKeyError: 'line' is not allowed in 'line'

我该怎么办?

2 个答案:

答案 0 :(得分:0)

text属性不是line属性。

类似的东西:

edge_trace = Scatter(
    x=[],
    y=[],
    line=Line(width=3, color='rgba(136, 136, 136, .8)'),
    hoverinfo='none',
    text=[],
    mode='lines')

for edge in G.edges():
    x0, y0 = pos[edge[0]]
    x1, y1 = pos[edge[1]]
    edge_trace['x'] += [x0, x1, None]
    edge_trace['y'] += [y0, y1, None]
    edge_trace['text'].append('label')

应该有用。

答案 1 :(得分:0)

如果尝试在边缘添加标签,则需要将模式设置为'lines+text',然后将text属性设置为标签列表。请参见下面的3d示例:

trace3 = go.Scatter3d(x=Xe2, y=Ye2, z=Ze2,
                         mode='lines+text',
                         line=dict(color='rgb(0,125,0)', width=4),
                         hoverinfo='none',
                         text=link_labels,
                         textposition='bottom right',
                         textfont=dict(
                             size=20
                         ),
                     )

link_labels是长度等于Xe2Ye2Ze2的列表。