Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1016 Bytes)

1
#!/usr/bin/env python
2
"""
3
==============
4
Weighted Graph
5
==============
6

7
An example using Graph as a weighted network.
8
"""
9
# Author: Aric Hagberg (hagberg@lanl.gov)
10
import matplotlib.pyplot as plt
11
import networkx as nx
12

    
13
G = nx.Graph()
14

    
15
G.add_edge('a', 'b', weight=0.6)
16
G.add_edge('a', 'c', weight=0.2)
17
G.add_edge('c', 'd', weight=0.1)
18
G.add_edge('c', 'e', weight=0.7)
19
G.add_edge('c', 'f', weight=0.9)
20
G.add_edge('a', 'd', weight=0.3)
21

    
22
elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 0.5]
23
esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] <= 0.5]
24

    
25
pos = nx.spring_layout(G)  # positions for all nodes
26

    
27
# nodes
28
nx.draw_networkx_nodes(G, pos, node_size=700)
29

    
30
# edges
31
nx.draw_networkx_edges(G, pos, edgelist=elarge,
32
                       width=6)
33
nx.draw_networkx_edges(G, pos, edgelist=esmall,
34
                       width=6, alpha=0.5, edge_color='b', style='dashed')
35

    
36
# labels
37
nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif')
38

    
39
plt.axis('off')
40
plt.show()