plot_write_dotfile.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. """
  3. =============
  4. Write Dotfile
  5. =============
  6. Write a dot file from a networkx graph for further processing with graphviz.
  7. You need to have either pygraphviz or pydot for this example.
  8. See https://networkx.github.io/documentation/latest/reference/drawing.html
  9. for more info.
  10. """
  11. # Author: Aric Hagberg (hagberg@lanl.gov)
  12. # Copyright (C) 2004-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. # and the following code block is not needed
  20. # but we want to see which module is used and
  21. # if and why it fails
  22. try:
  23. import pygraphviz
  24. from networkx.drawing.nx_agraph import write_dot
  25. print("using package pygraphviz")
  26. except ImportError:
  27. try:
  28. import pydot
  29. from networkx.drawing.nx_pydot import write_dot
  30. print("using package pydot")
  31. except ImportError:
  32. print()
  33. print("Both pygraphviz and pydot were not found ")
  34. print("see https://networkx.github.io/documentation/latest/reference/drawing.html")
  35. print()
  36. raise
  37. G = nx.grid_2d_graph(5, 5) # 5x5 grid
  38. write_dot(G, "grid.dot")
  39. print("Now run: neato -Tps grid.dot >grid.ps")