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.

31 lines
1.0KB

  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. import clifford
  8. import numpy as np
  9. VOP_COLORS = ["red", "green", "blue", "orange", "yellow", "purple", "black", "white"]
  10. def draw(graph, vops, filename="out.pdf", pos=None, ns=500):
  11. """ Draw a graph with networkx layout """
  12. plt.clf()
  13. g = nx.from_edgelist(edgelist(graph))
  14. pos = nx.spring_layout(g) if pos==None else pos
  15. colors = [VOP_COLORS[vop % len(VOP_COLORS)] for vop in vops]
  16. nx.draw_networkx_nodes(g, pos, node_color="white", node_size=ns)
  17. nx.draw_networkx_nodes(g, pos, node_color=colors, node_size=ns, alpha=.4)
  18. nx.draw_networkx_edges(g, pos, edge_color="gray")
  19. nx.draw_networkx_labels(g, pos, font_family="FreeSans")
  20. labels = {i: clifford.name_of(vops[i]) for i in g.nodes()}
  21. pos = {k: v + np.array([0, -.1]) for k, v in pos.items()}
  22. nx.draw_networkx_labels(g, pos, labels, font_family="FreeSans")
  23. plt.axis('off')
  24. plt.savefig(filename)
  25. return pos