Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (948 Bytes)

1
#!/usr/bin/env python
2
"""
3
================
4
Degree histogram
5
================
6

7
Draw degree histogram with matplotlib.
8
Random graph shown as inset
9
"""
10
import collections
11
import matplotlib.pyplot as plt
12
import networkx as nx
13

    
14
G = nx.gnp_random_graph(100, 0.02)
15

    
16
degree_sequence = sorted([d for n, d in G.degree()], reverse=True)  # degree sequence
17
# print "Degree sequence", degree_sequence
18
degreeCount = collections.Counter(degree_sequence)
19
deg, cnt = zip(*degreeCount.items())
20

    
21
fig, ax = plt.subplots()
22
plt.bar(deg, cnt, width=0.80, color='b')
23

    
24
plt.title("Degree Histogram")
25
plt.ylabel("Count")
26
plt.xlabel("Degree")
27
ax.set_xticks([d + 0.4 for d in deg])
28
ax.set_xticklabels(deg)
29

    
30
# draw graph in inset
31
plt.axes([0.4, 0.4, 0.5, 0.5])
32
Gcc = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)[0]
33
pos = nx.spring_layout(G)
34
plt.axis('off')
35
nx.draw_networkx_nodes(G, pos, node_size=20)
36
nx.draw_networkx_edges(G, pos, alpha=0.4)
37

    
38
plt.show()