6

I have built a graph using networkx and thereafter visualized it using plotly as described in the official guide. However, I get a random "new text" label in the graph for no apparent reason. This label doesn't appear in one particular zone of the network but it rather depends on where I am zooming (so it might appear on one part and then if I zoom another part, it will appear there).
I checked all labels (nodes or edges) but as expected there is no problem there.
I even checked for any hardcoded "new text" part in the code but everything looks fine.
What could be the issue here? enter image description here

Update

Here is the code used to visualize it:

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)
rpanai
  • 12,515
  • 2
  • 42
  • 64
Bendemann
  • 735
  • 11
  • 31
  • 2
    Did you try making a different *toy* network and graph it using your graphing code? - Is it still there? When you say you `checked for` that text did you go through it and look for it or did you use a search function in an editor? – wwii Apr 12 '20 at 15:27
  • I copy and pasted the complete example from the page you linked to and I don't see that `new text` anywhere in the plot. – wwii Apr 12 '20 at 15:40
  • 2
    We'll need to see the code you ran in order to help. – Joel Apr 13 '20 at 00:10
  • @Joel I added the code – Bendemann Apr 13 '20 at 19:11
  • @wwii I just tried that. Actually I slightly modified the code building the graph such that it has only 5 edges (out of the original 365) and it seems like the "new text" label is gone. If I add back the edges it comes right back. I am very positive that no edge has "new text" label though. Also, once I zoom in the graph, the "new text" label will reappear once I use the "pan" option of plotly. – Bendemann Apr 13 '20 at 19:14
  • Can you provide a complete example? What is `G2`? – Keldorn Apr 27 '20 at 06:21

1 Answers1

6

Ok, so I figured this out. Just in case someone will ever stumble on this, the problem was on declaring the annotations_list variable which I actually use to specify the labels of the edges. However, as you can see from the code I am labeling only some of the edges:

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

The thing that I failed to correctly set here was the actual label of the edge text = label[0] which should rather be text = "" if label[0] == None else label[0].
It looks like by default a 'new text' label will be set if the text field of the annotation is set to None.

Bendemann
  • 735
  • 11
  • 31