Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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)