networkx节点2D网格,Python

x33g5p2x  于2021-11-11 转载在 Python  
字(0.7k)|赞(0)|评价(0)|浏览(327)

此种类型2D网格图,类似于棋盘等。

  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3. def my_graph():
  4. G = nx.grid_2d_graph(4, 4)
  5. pos = nx.spring_layout(G, iterations=100)
  6. # nrows=2,ncols=2,index=1
  7. plt.subplot(2, 2, 1)
  8. nx.draw(G, pos, font_size=10, with_labels=True)
  9. # nrows=2,ncols=2,index=2
  10. plt.subplot(2, 2, 2)
  11. nx.draw(G, pos, node_color="yellow", node_size=50, with_labels=False)
  12. # nrows=2,ncols=2,index=3
  13. plt.subplot(2, 2, 3)
  14. H = G.to_directed()
  15. nx.draw(H, pos, node_color="blue", node_size=20, with_labels=False)
  16. # nrows=2,ncols=2,index=4
  17. plt.subplot(2, 2, 4)
  18. pos = dict((n, n) for n in G.nodes())
  19. labels = dict(((i, j), 'Phil') for i, j in G.nodes())
  20. nx.draw_networkx(G, pos=pos, labels=labels, font_size=8, font_color='white', node_color="red", node_size=350,
  21. width=3)
  22. plt.axis('off')
  23. plt.show()

如图:

相关文章