Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
927B

  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", "orange", "yellow", "purple", "black", "white"]
  8. def draw(graph, vops, filename="out.pdf", ns=500):
  9. """ Draw a graph with networkx layout """
  10. plt.clf()
  11. g = nx.from_edgelist(edgelist(graph))
  12. pos = nx.spring_layout(g)
  13. colors = [VOP_COLORS[vop % len(VOP_COLORS)] for vop in vops]
  14. nx.draw_networkx_nodes(g, pos, node_color="white", node_size=ns)
  15. nx.draw_networkx_nodes(g, pos, node_color=colors, node_size=ns, alpha=.4)
  16. nx.draw_networkx_labels(g, pos)
  17. nx.draw_networkx_edges(g, pos)
  18. plt.axis('off')
  19. plt.savefig(filename)
  20. if __name__ == '__main__':
  21. g, vops = graph()
  22. add_edge(g, 0, 1)
  23. add_edge(g, 1, 3)
  24. add_edge(g, 3, 2)
  25. add_edge(g, 3, 0)
  26. add_edge(g, 2, 0)
  27. edgelist(g)
  28. draw(g, vops)