networkx绘制拓扑图节点的边和权,Python

x33g5p2x  于2021-11-11 转载在 Python  
字(0.9k)|赞(0)|评价(0)|浏览(513)
  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3. def my_graph():
  4. G = nx.cubical_graph()
  5. pos = nx.spring_layout(G)
  6. options = {"node_size": 400, "alpha": 0.8}
  7. nx.draw_networkx_nodes(G, pos,
  8. nodelist=[0, 1, 2, 3],
  9. node_color="r",
  10. **options)
  11. nx.draw_networkx_nodes(G, pos,
  12. nodelist=[4, 5, 6, 7],
  13. node_color="b",
  14. **options)
  15. nx.draw_networkx_edges(G, pos,
  16. width=2.0,
  17. alpha=0.5)
  18. nx.draw_networkx_edges(G, pos,
  19. edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)],
  20. width=5,
  21. alpha=0.5,
  22. edge_color="r")
  23. nx.draw_networkx_edges(G, pos,
  24. edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],
  25. width=8,
  26. alpha=0.5,
  27. edge_color="g")
  28. labels = {}
  29. labels[0] = r"a"
  30. labels[1] = r"b"
  31. labels[2] = r"c"
  32. labels[3] = r"d"
  33. labels[4] = r"AA"
  34. labels[5] = r"BB"
  35. labels[6] = r"CC"
  36. labels[7] = r"DD"
  37. nx.draw_networkx_labels(G, pos,
  38. labels,
  39. font_size=15)
  40. plt.axis("off")
  41. plt.show()

输出:

相关文章