plot_pygraphviz_simple.py 951 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python
  2. """
  3. =================
  4. Pygraphviz Simple
  5. =================
  6. An example showing how to use the interface to the pygraphviz
  7. AGraph class to convert to and from graphviz.
  8. Also see the pygraphviz documentation and examples at
  9. http://pygraphviz.github.io/
  10. """
  11. # Author: Aric Hagberg (hagberg@lanl.gov)
  12. # Copyright (C) 2006-2019 by
  13. # Aric Hagberg <hagberg@lanl.gov>
  14. # Dan Schult <dschult@colgate.edu>
  15. # Pieter Swart <swart@lanl.gov>
  16. # All rights reserved.
  17. # BSD license.
  18. import networkx as nx
  19. # plain graph
  20. G = nx.complete_graph(5) # start with K5 in networkx
  21. A = nx.nx_agraph.to_agraph(G) # convert to a graphviz graph
  22. X1 = nx.nx_agraph.from_agraph(A) # convert back to networkx (but as Graph)
  23. X2 = nx.Graph(A) # fancy way to do conversion
  24. G1 = nx.Graph(X1) # now make it a Graph
  25. A.write('k5.dot') # write to dot file
  26. X3 = nx.nx_agraph.read_dot('k5.dot') # read from dotfile