0

I have made a network (Graph of networkX) and want to use plotly.graph_objects for visualization. In the edge of the graph, I have added some information (str type).

For example, information is stored as below. G[Node1][Node2]['info'] = "ABC"

And Now I want to show this information in the plotly. previously, I add edge label with matplotlib like this. enter image description here

But In the plotly Scatter3D, I cannot find how to show this information.

below are my code to show information.

# Example Graph G
G = nx.Graph()
G.add_nodes_from([0, 1, 2, 3])
G.add_edge(1, 2, Type = 'ABC')
G.add_edge(2, 0, Type = '')
G.add_edge(2, 3, Type = '')

# random location for nodes
spring_3D = nx.spring_layout(G, dim=3, seed=18)

x_nodes = [spring_3D[i][0] for i in range(len(G.nodes()))]
y_nodes = [spring_3D[i][1] for i in range(len(G.nodes()))]
z_nodes = [spring_3D[i][2] for i in range(len(G.nodes()))]

# make edge trace
x_edges = []
y_edges = []
z_edges = []
edge_text = []

for edge in G.edges():
    x_coords = [spring_3D[edge[0]][0], spring_3D[edge[1]][0], None]
    x_edges += x_coords

    y_coords = [spring_3D[edge[0]][1], spring_3D[edge[1]][1], None]
    y_edges += y_coords

    z_coords = [spring_3D[edge[0]][2], spring_3D[edge[1]][2], None]
    z_edges += z_coords

    edge_text.append(G[edge[0]][edge[1]]['Type'])

trace_edges = go.Scatter3d(x = x_edges,
                           y = y_edges,
                           z = z_edges,
                           mode = 'lines',
                           line = dict(color='black', width=2),
                           text = edge_text,
                           # I use text property, because with trace_nodes, 
                           # text show node labels as exactly what I want
                           hoverinfo = 'none')

trace_nodes = go.Scatter3d(x=x_nodes,
                           y=y_nodes,
                           z=z_nodes,
                           mode='markers')

layout = go.Layout(title='title',
                   width=500, height=500,
                   showlegend=False,
                   hovermode='closest')

data = [trace_edges, trace_nodes]
fig = go.figure(data=data, layout=layout)

fig.show()
                           

How can I do this task? (info text can be shown permanently like image, or can be shown only when mouse is located on the edge like node text)

JY Kim
  • 11
  • 4
  • I have never used Networkx on a regular basis, so I needed data and code. I found this page from a search and added string annotations in Plotly from [this example](https://plotly.com/python/3d-scatter-plots/#3d-scatter-plot-with-colorscaling-and-marker-styling). The text on the 3D scatterplot in the official reference example was not valid. So I had to add the text using annotations, so I expanded the previous page in [Colab](https://colab.research.google.com/drive/1zl_XRM69YTcKgHfvkpohGTnVPS7sx4zP?usp=sharing) and added annotations in plotly. I will answer if this code is what you intended. – r-beginners Apr 10 '22 at 07:07
  • dear r-beginners, I edited my post to give example graph! Thanks! – JY Kim Apr 11 '22 at 00:53

0 Answers0