Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1.15 KB)

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

7
Draw a graph with directed edges using a colormap and different node sizes.
8

9
Edges have different colors and alphas (opacity). Drawn using matplotlib.
10
"""
11
# Author: Rodrigo Dorantes-Gilardi (rodgdor@gmail.com)
12

    
13
from __future__ import division
14
import matplotlib as mpl
15
import matplotlib.pyplot as plt
16
import networkx as nx
17

    
18
G = nx.generators.directed.random_k_out_graph(10, 3, 0.5)
19
pos = nx.layout.spring_layout(G)
20

    
21
node_sizes = [3 + 10 * i for i in range(len(G))]
22
M = G.number_of_edges()
23
edge_colors = range(2, M + 2)
24
edge_alphas = [(5 + i) / (M + 4) for i in range(M)]
25

    
26
nodes = nx.draw_networkx_nodes(G, pos, node_size=node_sizes, node_color='blue')
27
edges = nx.draw_networkx_edges(G, pos, node_size=node_sizes, arrowstyle='->',
28
                               arrowsize=10, edge_color=edge_colors,
29
                               edge_cmap=plt.cm.Blues, width=2)
30
# set alpha value for each edge
31
for i in range(M):
32
    edges[i].set_alpha(edge_alphas[i])
33

    
34
pc = mpl.collections.PatchCollection(edges, cmap=plt.cm.Blues)
35
pc.set_array(edge_colors)
36
plt.colorbar(pc)
37

    
38
ax = plt.gca()
39
ax.set_axis_off()
40
plt.show()