File: Examples.txt

package info (click to toggle)
python-networkx 0.32-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,332 kB
  • ctags: 1,020
  • sloc: python: 21,197; makefile: 67; sh: 11
file content (59 lines) | stat: -rw-r--r-- 892 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
NetworkX Examples
=================

See the examples at
https://networkx.lanl.gov/browser/networkx/trunk/doc/examples/

And the gallery at
https://networkx.lanl.gov/wiki/gallery

Basic
-----

>>> from networkx import *
>>> G=Graph()
>>> G.add_edge(1,2)
>>> G.add_edge(2,3)
>>> G.add_edge(2,4)
>>> G.add_edge(2,5)
>>> G.add_edge(2,6)
>>> G.add_edge(4,6)

Print the nodes of the graph

>>> print G.nodes()
[1, 2, 3, 4, 5, 6]

Print the degree of each node

>>> for n in G:
...     print n,G.degree(n)
... 
1 1
2 5
3 1
4 2
5 1
6 2

Draw with matplotlib

>>> draw(G)

.. raw:: html

    <img alt="NetworkX art" class="image" height="600"
    src="https://networkx.lanl.gov/images/basic-s.png"  width="800" />

More elaborate drawings are also possible

.. raw:: html

    <img alt="NetworkX art" class="image" height="800"
    src="https://networkx.lanl.gov/images/as.png"  width="800" />