plot_read_write.py 525 B

123456789101112131415161718192021222324
  1. """
  2. ======================
  3. Read and write graphs.
  4. ======================
  5. Read and write graphs.
  6. """
  7. import matplotlib.pyplot as plt
  8. import networkx as nx
  9. G = nx.grid_2d_graph(5, 5) # 5x5 grid
  10. # print the adjacency list
  11. for line in nx.generate_adjlist(G):
  12. print(line)
  13. # write edgelist to grid.edgelist
  14. nx.write_edgelist(G, path="grid.edgelist", delimiter=":")
  15. # read edgelist from grid.edgelist
  16. H = nx.read_edgelist(path="grid.edgelist", delimiter=":")
  17. pos = nx.spring_layout(H, seed=200)
  18. nx.draw(H, pos)
  19. plt.show()