Anders and Briegel in Python
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
849B

  1. """
  2. Utility function for plotting graphs nicely
  3. """
  4. import networkx as nx
  5. from matplotlib import pyplot as plt
  6. from graph import *
  7. VOP_COLORS = ["red", "green", "blue"]
  8. def draw(graph, vops, filename="out.pdf", ns=500):
  9. """ Draw a graph with networkx layout """
  10. g = nx.from_edgelist(edgelist(graph))
  11. pos = nx.spring_layout(g)
  12. colors = [VOP_COLORS[vop] for vop in vops]
  13. nx.draw_networkx_nodes(g, pos, node_color="white", node_size=ns)
  14. nx.draw_networkx_nodes(g, pos, node_color=colors, node_size=ns, alpha=.4)
  15. nx.draw_networkx_labels(g, pos)
  16. nx.draw_networkx_edges(g, pos)
  17. plt.axis('off')
  18. plt.savefig(filename)
  19. if __name__ == '__main__':
  20. g, vops = graph(10)
  21. add_edge(g, 0, 1)
  22. add_edge(g, 1, 3)
  23. add_edge(g, 3, 2)
  24. add_edge(g, 3, 0)
  25. add_edge(g, 2, 0)
  26. edgelist(g)
  27. draw(g, vops)