Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / pygraphviz / plot_pygraphviz_attributes.py @ 5cef0f13

History | View | Annotate | Download (1.07 KB)

1
#!/usr/bin/env python
2
"""
3
=====================
4
Pygraphviz Attributes
5
=====================
6

7
An example showing how to use the interface to the pygraphviz
8
AGraph class to convert to and from graphviz.
9

10
Also see the pygraphviz documentation and examples at
11
http://pygraphviz.github.io/
12
"""
13
# Author: Aric Hagberg (hagberg@lanl.gov)
14

    
15
#    Copyright (C) 2006-2019 by
16
#    Aric Hagberg <hagberg@lanl.gov>
17
#    Dan Schult <dschult@colgate.edu>
18
#    Pieter Swart <swart@lanl.gov>
19
#    All rights reserved.
20
#    BSD license.
21

    
22
import networkx as nx
23

    
24
# networkx graph
25
G = nx.Graph()
26
# ad edges with red color
27
G.add_edge(1, 2, color='red')
28
G.add_edge(2, 3, color='red')
29
# add nodes 3 and 4
30
G.add_node(3)
31
G.add_node(4)
32

    
33
# convert to a graphviz agraph
34
A = nx.nx_agraph.to_agraph(G)
35

    
36
# write to dot file
37
A.write('k5_attributes.dot')
38

    
39
# convert back to networkx Graph with attributes on edges and
40
# default attributes as dictionary data
41
X = nx.nx_agraph.from_agraph(A)
42
print("edges")
43
print(list(X.edges(data=True)))
44
print("default graph attributes")
45
print(X.graph)
46
print("node node attributes")
47
print(X.node)