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.

26 lines
1.0KB

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