plot_four_grids.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """
  2. ==========
  3. Four Grids
  4. ==========
  5. Draw a 4x4 graph with matplotlib.
  6. This example illustrates the use of keyword arguments to `networkx.draw` to
  7. customize the visualization of a simple Graph comprising a 4x4 grid.
  8. """
  9. import matplotlib.pyplot as plt
  10. import networkx as nx
  11. G = nx.grid_2d_graph(4, 4) # 4x4 grid
  12. pos = nx.spring_layout(G, iterations=100, seed=39775)
  13. # Create a 2x2 subplot
  14. fig, all_axes = plt.subplots(2, 2)
  15. ax = all_axes.flat
  16. nx.draw(G, pos, ax=ax[0], font_size=8)
  17. nx.draw(G, pos, ax=ax[1], node_size=0, with_labels=False)
  18. nx.draw(
  19. G,
  20. pos,
  21. ax=ax[2],
  22. node_color="tab:green",
  23. edgecolors="tab:gray", # Node surface color
  24. edge_color="tab:gray", # Color of graph edges
  25. node_size=250,
  26. with_labels=False,
  27. width=6,
  28. )
  29. H = G.to_directed()
  30. nx.draw(
  31. H,
  32. pos,
  33. ax=ax[3],
  34. node_color="tab:orange",
  35. node_size=20,
  36. with_labels=False,
  37. arrowsize=10,
  38. width=2,
  39. )
  40. # Set margins for the axes so that nodes aren't clipped
  41. for a in ax:
  42. a.margins(0.10)
  43. fig.tight_layout()
  44. plt.show()