Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / drawing / plot_spectral_grid.py @ 5cef0f13

History | View | Annotate | Download (1.57 KB)

1
"""
2
==================
3
Spectral Embedding
4
==================
5

6
The spectral layout positions the nodes of the graph based on the
7
eigenvectors of the graph Laplacian $L = D - A$, where $A$ is the
8
adjacency matrix and $D$ is the degree matrix of the graph.
9
By default, the spectral layout will embed the graph in two
10
dimensions (you can embed your graph in other dimensions using the
11
``dim`` argument to either :func:`~drawing.nx_pylab.draw_spectral` or
12
:func:`~drawing.layout.spectral_layout`).
13

14
When the edges of the graph represent similarity between the incident
15
nodes, the spectral embedding will place highly similar nodes closer
16
to one another than nodes which are less similar.
17

18
This is particularly striking when you spectrally embed a grid
19
graph.  In the full grid graph, the nodes in the center of the
20
graph are pulled apart more than nodes on the periphery.
21
As you remove internal nodes, this effect increases.
22
"""
23

    
24
import matplotlib.pyplot as plt
25
import networkx as nx
26

    
27

    
28
options = {
29
    'node_color': 'C0',
30
    'node_size': 100,
31
}
32

    
33
G = nx.grid_2d_graph(6, 6)
34
plt.subplot(332)
35
nx.draw_spectral(G, **options)
36

    
37
G.remove_edge((2, 2), (2, 3))
38
plt.subplot(334)
39
nx.draw_spectral(G, **options)
40

    
41
G.remove_edge((3, 2), (3, 3))
42
plt.subplot(335)
43
nx.draw_spectral(G, **options)
44

    
45
G.remove_edge((2, 2), (3, 2))
46
plt.subplot(336)
47
nx.draw_spectral(G, **options)
48

    
49
G.remove_edge((2, 3), (3, 3))
50
plt.subplot(337)
51
nx.draw_spectral(G, **options)
52

    
53
G.remove_edge((1, 2), (1, 3))
54
plt.subplot(338)
55
nx.draw_spectral(G, **options)
56

    
57
G.remove_edge((4, 2), (4, 3))
58
plt.subplot(339)
59
nx.draw_spectral(G, **options)
60

    
61
plt.show()