networkx节点node样式style定制化,Python

x33g5p2x  于2021-12-30 转载在 Python  
字(0.9k)|赞(0)|评价(0)|浏览(252)

networkx节点node样式style定制化,Python

  1. import networkx as nx
  2. from matplotlib import pyplot as plt
  3. KEY = 'key'
  4. def app():
  5. G = nx.DiGraph()
  6. plt.figure(figsize=(12, 10), dpi=100)
  7. G.add_node(G.number_of_nodes())
  8. G.add_node(G.number_of_nodes())
  9. G.add_node(G.number_of_nodes())
  10. G.nodes[0][KEY] = [(90, 1), (80, 2), (70, 3)]
  11. G.nodes[1][KEY] = [(91, 1), (81, 2), (71, 3)]
  12. G.nodes[2][KEY] = [(92, 1), (82, 2), (72, 3)]
  13. print(G.nodes(data=True))
  14. pos = nx.spring_layout(G)
  15. labels = {}
  16. for i in range(G.number_of_nodes()):
  17. ls = G.nodes[i][KEY]
  18. s = str(i) + '\n' + tuplelist2str(ls)
  19. labels.__setitem__(i, s)
  20. nx.draw_networkx_nodes(G, pos=pos,
  21. node_color='red',
  22. node_size=2000,
  23. node_shape='s')
  24. label_options = {"ec": "black", "fc": "white", "alpha": 0.7}
  25. nx.draw_networkx_labels(G, pos=pos,
  26. labels=labels,
  27. font_size=8,
  28. font_color='blue',
  29. bbox=label_options)
  30. plt.margins(0.1)
  31. plt.axis('off')
  32. plt.show()
  33. def tuplelist2str(tl):
  34. s = ''
  35. for t in tl:
  36. s = s + '(' + str(t[0]) + ',' + str(t[1]) + ')'
  37. return s
  38. if __name__ == '__main__':
  39. app()

输出:

相关文章