How to show text information on the lines of Plotly Scatter3d?

366 views Asked by At

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)

0

There are 0 answers