plot_pygraphviz_attributes.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python
  2. """
  3. =====================
  4. Pygraphviz Attributes
  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. # networkx graph
  20. G = nx.Graph()
  21. # ad edges with red color
  22. G.add_edge(1, 2, color='red')
  23. G.add_edge(2, 3, color='red')
  24. # add nodes 3 and 4
  25. G.add_node(3)
  26. G.add_node(4)
  27. # convert to a graphviz agraph
  28. A = nx.nx_agraph.to_agraph(G)
  29. # write to dot file
  30. A.write('k5_attributes.dot')
  31. # convert back to networkx Graph with attributes on edges and
  32. # default attributes as dictionary data
  33. X = nx.nx_agraph.from_agraph(A)
  34. print("edges")
  35. print(list(X.edges(data=True)))
  36. print("default graph attributes")
  37. print(X.graph)
  38. print("node node attributes")
  39. print(X.nodes.data(True))