python - "new text"标签随机出现在 Plotly 图中

标签 python plotly networkx plotly-python

我已经使用 networkx 构建了一个图表然后使用 plotly 将其可视化如 official guide 中所述.但是,我无缘无故地在图中得到了一个随机的“新文本”标签。这个标签不会出现在网络的某个特定区域,而是取决于我缩放的位置(所以它可能会出现在一个部分,然后如果我缩放另一部分,它会出现在那里)。
我检查了所有标签(节点或边),但正如预期的那样,那里没有问题。
我什至检查了代码中是否有任何硬编码的“新文本”部分,但一切看起来都很好。
这里可能是什么问题? enter image description here

更新

这是用于可视化它的代码:

import networkx
import plotly.graph_objs as go
# set node positions
pos = nx.nx_pydot.graphviz_layout(G2)

# Nodes information
node_x = []
node_y = []
node_labels = []
for key, value in pos.items():
    x, y = value[0], value[1]
    node_x.append(x)
    node_y.append(y)
    node_labels.append(key)

# Edges information
edge_x = []
edge_y = []
edge_labels = []

for edge in G2.edges().data():
    x0, y0 = pos[edge[0]]
    x1, y1 = pos[edge[1]]
    edge_x.append(x0) 
    edge_x.append(x1)
    edge_x.append(None)
    edge_y.append(y0)
    edge_y.append(y1)
    edge_y.append(None)

    # Get the edge label
    label = [val for val in edge[2].values()]

    # Get the middle line coordinates where edge's name is added
    ax = (x0+x1)/2
    ay = (y0+y1)/2

    # Not all edges have a label
    if len(label) > 0:
        edge_labels.append((label[0], ax, ay))
    else:
        edge_labels.append((None, None, None))


# create node trace:
node_trace = go.Scatter( x=node_x, y=node_y, text = node_labels, textposition='bottom center',
                mode='markers+text', hoverinfo='text', name = 'Nodes',
                marker=dict( showscale=False,
#                 symbol='circle',
                color='cyan',
                size=15,
                line=dict(color='rgb(180,255,255)', width=1))
                       )


# create edge trace:
edge_trace = go.Scatter( x=edge_x, y=edge_y,
    mode = 'lines', line=dict(width=1, color='rgb(90, 90, 90)'),
    hoverinfo='none',)
# Annotations for edge's labels
annotations_list = [
    dict(
        x = None if label[0] == None else label[1],
        y = None if label[0] == None else label[2],
        xref = 'x',
        yref = 'y',
        text = label[0],
        showarrow=False,
        opacity=0.7,
        ax = label[1],
        ay = label[2]
    )
    for label in edge_labels
]

data = [edge_trace, node_trace]
layout = go.Layout(
                title='FOL Network Graph',
                titlefont_size=20,
                width = 700,
                height = 600,
                showlegend=False,
                plot_bgcolor="rgb(255, 255, 250)",
                hovermode='closest',
                clickmode='event+select',
                margin=dict(b=20,l=5,r=5,t=40),
                annotations=annotations_list,
                xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
                yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
)

fig = go.Figure(data=data, layout=layout)

最佳答案

好的,所以我想通了。以防万一有人偶然发现这一点,问题在于声明 annotations_list我实际用来指定边缘标签的变量。但是,正如您从代码中看到的,我只标记了一些边缘:

x = None if label[0] == None else label[1],
y = None if label[0] == None else label[2], 

我在这里没有正确设置的是边缘的实际标签text = label[0]应该是 text = "" if label[0] == None else label[0] .
如果 text,默认情况下将设置“新文本”标签。注释的字段设置为 None .

关于python - "new text"标签随机出现在 Plotly 图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61173519/

相关文章:

python - 在defaultdict中把一个元素放在正确的位置

python - 如何强制 Google App Engine [python] 使用 SSL (https)?

python - 如何防止 AppEngine 内存缓存刷新

python - 如何使用 plotly_express 绘制多折线图?

javascript - Plotly Js Choropleth Iso2 而不是 Iso3

python - 在街道数据(图表)中查找社区(集团)

python - 在有向加权图中恰好有 k 条边的最短路径生成(编辑 : visit each node only once)

python - 对同级目录使用 pathlib 的 relative_to

python - 使用 selenium webdriver 登录网站时出错

python - 使用袖扣设置多个带有移动平均线的子图并离线绘制