Anders and Briegel in Python
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

31 行
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