plot_center_node.py 621 B

1234567891011121314151617181920
  1. """
  2. ====================
  3. Custom Node Position
  4. ====================
  5. Draw a graph with node(s) located at user-defined positions.
  6. When a position is set by the user, the other nodes can still be neatly organised in a layout.
  7. """
  8. import networkx as nx
  9. import numpy as np
  10. G = nx.path_graph(20) # An example graph
  11. center_node = 5 # Or any other node to be in the center
  12. edge_nodes = set(G) - {center_node}
  13. # Ensures the nodes around the circle are evenly distributed
  14. pos = nx.circular_layout(G.subgraph(edge_nodes))
  15. pos[center_node] = np.array([0, 0]) # manually specify node position
  16. nx.draw(G, pos, with_labels=True)