Thursday, 25 February 2021

How to iteratively change nodes position Networkx

I have this graph:

enter image description here

I would like to change the position of the nodes by dragging them with the mouse. If it were possible it would be better to do it from the console I am using Google colab for this project.

This is my code:

nodes = np.array(['A', 'B', 'C', 'D', 'E', 'F', 'G'])
edges = np.array([['A', 'B'], ['A', 'C'], ['B', 'D'], ['B', 'E'], ['C', 'F'], ['C', 'G']])
pos = np.array([[0, 0], [-2, 1], [2, 1], [-3, 2], [-1, 2], [1, 2], [3, 2]])

G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)

plt.figure(3,figsize=(12,12)) 
pos = nx.spring_layout(G) 
nx.draw(
    G,
    pos=pos,
    node_color='#FF0000',
    with_labels=True
)

I tried using:

import networkx as nx
import netgraph
I = netgraph.InteractiveGraph(G)

But on Google Colab it doesn't work.

How could this be done?



from How to iteratively change nodes position Networkx

No comments:

Post a Comment