Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1.24 KB)

1
#!/usr/bin/env python
2
"""
3
=============
4
Write Dotfile
5
=============
6

7

8
Write a dot file from a networkx graph for further processing with graphviz.
9

10
You need to have either pygraphviz or pydot for this example.
11

12
See https://networkx.github.io/documentation/latest/reference/drawing.html
13
for more info.
14

15
"""
16
# Author: Aric Hagberg (hagberg@lanl.gov)
17

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

    
25
import networkx as nx
26

    
27
# and the following code block is not needed
28
# but we want to see which module is used and
29
# if and why it fails
30
try:
31
    import pygraphviz
32
    from networkx.drawing.nx_agraph import write_dot
33
    print("using package pygraphviz")
34
except ImportError:
35
    try:
36
        import pydot
37
        from networkx.drawing.nx_pydot import write_dot
38
        print("using package pydot")
39
    except ImportError:
40
        print()
41
        print("Both pygraphviz and pydot were not found ")
42
        print("see  https://networkx.github.io/documentation/latest/reference/drawing.html")
43
        print()
44
        raise
45

    
46
G = nx.grid_2d_graph(5, 5)  # 5x5 grid
47
write_dot(G, "grid.dot")
48
print("Now run: neato -Tps grid.dot >grid.ps")