Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / drawing / tests / test_pylab.py @ 5cef0f13

History | View | Annotate | Download (6.94 KB)

1
"""Unit tests for matplotlib drawing functions."""
2
import os
3
import itertools
4
from nose import SkipTest
5
import networkx as nx
6

    
7

    
8
class TestPylab(object):
9
    @classmethod
10
    def setupClass(cls):
11
        global plt
12
        try:
13
            import matplotlib as mpl
14
            mpl.use('PS', warn=False)
15
            import matplotlib.pyplot as plt
16
            plt.rcParams['text.usetex'] = False
17
        except ImportError:
18
            raise SkipTest('matplotlib not available.')
19
        except RuntimeError:
20
            raise SkipTest('matplotlib not available.')
21

    
22
    def setUp(self):
23
        self.G = nx.barbell_graph(4, 6)
24

    
25
    def test_draw(self):
26
        try:
27
            functions = [nx.draw_circular,
28
                         nx.draw_kamada_kawai,
29
                         nx.draw_planar,
30
                         nx.draw_random,
31
                         nx.draw_spectral,
32
                         nx.draw_spring,
33
                         nx.draw_shell]
34
            options = [{
35
                'node_color': 'black',
36
                'node_size': 100,
37
                'width': 3,
38
            }]
39
            for function, option in itertools.product(functions, options):
40
                function(self.G, **option)
41
                plt.savefig('test.ps')
42

    
43
        finally:
44
            try:
45
                os.unlink('test.ps')
46
            except OSError:
47
                pass
48

    
49
    def test_edge_colormap(self):
50
        colors = range(self.G.number_of_edges())
51
        nx.draw_spring(self.G, edge_color=colors, width=4,
52
                       edge_cmap=plt.cm.Blues, with_labels=True)
53
        plt.show()
54

    
55
    def test_arrows(self):
56
        nx.draw_spring(self.G.to_directed())
57
        plt.show()
58

    
59
    def test_edge_colors_and_widths(self):
60
        pos = nx.circular_layout(self.G)
61
        for G in (self.G, self.G.to_directed()):
62
            nx.draw_networkx_nodes(G, pos, node_color=[(1.0, 1.0, 0.2, 0.5)])
63
            nx.draw_networkx_labels(G, pos)
64
            # edge with default color and width
65
            nx.draw_networkx_edges(G, pos, edgelist=[(0, 1)],
66
                                   width=None,
67
                                   edge_color=None)
68
            # edges with global color strings and widths in lists
69
            nx.draw_networkx_edges(G, pos, edgelist=[(0, 2), (0, 3)],
70
                                   width=[3],
71
                                   edge_color=['r'])
72
            # edges with color strings and widths for each edge
73
            nx.draw_networkx_edges(G, pos, edgelist=[(0, 2), (0, 3)],
74
                                   width=[1, 3],
75
                                   edge_color=['r', 'b'])
76
            # edges with fewer color strings and widths than edges
77
            nx.draw_networkx_edges(G, pos,
78
                                   edgelist=[(1, 2), (1, 3), (2, 3), (3, 4)],
79
                                   width=[1, 3],
80
                                   edge_color=['g', 'm', 'c'])
81
            # edges with more color strings and widths than edges
82
            nx.draw_networkx_edges(G, pos, edgelist=[(3, 4)],
83
                                   width=[1, 2, 3, 4],
84
                                   edge_color=['r', 'b', 'g', 'k'])
85
            # with rgb tuple and 3 edges - is interpreted with cmap
86
            nx.draw_networkx_edges(G, pos, edgelist=[(4, 5), (5, 6), (6, 7)],
87
                                   edge_color=(1.0, 0.4, 0.3))
88
            # with rgb tuple in list
89
            nx.draw_networkx_edges(G, pos, edgelist=[(7, 8), (8, 9)],
90
                                   edge_color=[(0.4, 1.0, 0.0)])
91
            # with rgba tuple and 4 edges - is interpretted with cmap
92
            nx.draw_networkx_edges(G, pos, edgelist=[(9, 10), (10, 11),
93
                                   (10, 12), (10, 13)],
94
                                   edge_color=(0.0, 1.0, 1.0, 0.5))
95
            # with rgba tuple in list
96
            nx.draw_networkx_edges(G, pos, edgelist=[(9, 10), (10, 11),
97
                                   (10, 12), (10, 13)],
98
                                   edge_color=[(0.0, 1.0, 1.0, 0.5)])
99
            # with color string and global alpha
100
            nx.draw_networkx_edges(G, pos, edgelist=[(11, 12), (11, 13)],
101
                                   edge_color='purple', alpha=0.2)
102
            # with color string in a list
103
            nx.draw_networkx_edges(G, pos, edgelist=[(11, 12), (11, 13)],
104
                                   edge_color=['purple'])
105
            # with single edge and hex color string
106
            nx.draw_networkx_edges(G, pos, edgelist=[(12, 13)],
107
                                   edge_color='#1f78b4f0')
108

    
109
            plt.show()
110

    
111
    def test_labels_and_colors(self):
112
        G = nx.cubical_graph()
113
        pos = nx.spring_layout(G)  # positions for all nodes
114
        # nodes
115
        nx.draw_networkx_nodes(G, pos,
116
                               nodelist=[0, 1, 2, 3],
117
                               node_color='r',
118
                               node_size=500,
119
                               alpha=0.75)
120
        nx.draw_networkx_nodes(G, pos,
121
                               nodelist=[4, 5, 6, 7],
122
                               node_color='b',
123
                               node_size=500,
124
                               alpha=[0.25, 0.5, 0.75, 1.0])
125
        # edges
126
        nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)
127
        nx.draw_networkx_edges(G, pos,
128
                               edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)],
129
                               width=8, alpha=0.5, edge_color='r')
130
        nx.draw_networkx_edges(G, pos,
131
                               edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],
132
                               width=8, alpha=0.5, edge_color='b')
133
        # some math labels
134
        labels = {}
135
        labels[0] = r'$a$'
136
        labels[1] = r'$b$'
137
        labels[2] = r'$c$'
138
        labels[3] = r'$d$'
139
        labels[4] = r'$\alpha$'
140
        labels[5] = r'$\beta$'
141
        labels[6] = r'$\gamma$'
142
        labels[7] = r'$\delta$'
143
        nx.draw_networkx_labels(G, pos, labels, font_size=16)
144
        plt.show()
145

    
146
    def test_axes(self):
147
        fig, ax = plt.subplots()
148
        nx.draw(self.G, ax=ax)
149

    
150
    def test_empty_graph(self):
151
        G = nx.Graph()
152
        nx.draw(G)
153

    
154
    def test_multigraph_edgelist_tuples(self):
155
        # See Issue #3295
156
        G = nx.path_graph(3, create_using=nx.MultiDiGraph)
157
        nx.draw_networkx(G, edgelist=[(0, 1, 0)])
158
        nx.draw_networkx(G, edgelist=[(0, 1, 0)], node_size=[10, 20])
159

    
160
    def test_alpha_iter(self):
161
        pos = nx.random_layout(self.G)
162
        # with fewer alpha elements than nodes
163
        plt.subplot(131)
164
        nx.draw_networkx_nodes(self.G, pos, alpha=[0.1, 0.2])
165
        # with equal alpha elements and nodes
166
        num_nodes = len(self.G.nodes)
167
        alpha = [x / num_nodes for x in range(num_nodes)]
168
        colors = range(num_nodes)
169
        plt.subplot(132)
170
        nx.draw_networkx_nodes(self.G, pos, node_color=colors, alpha=alpha)
171
        # with more alpha elements than nodes
172
        alpha.append(1)
173
        plt.subplot(133)
174
        nx.draw_networkx_nodes(self.G, pos, alpha=alpha)