comparison env/lib/python3.9/site-packages/networkx/drawing/tests/test_pylab.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Unit tests for matplotlib drawing functions."""
2 import os
3 import itertools
4 import pytest
5
6 mpl = pytest.importorskip("matplotlib")
7 mpl.use("PS")
8 plt = pytest.importorskip("matplotlib.pyplot")
9 plt.rcParams["text.usetex"] = False
10
11 import networkx as nx
12
13
14 class TestPylab:
15 @classmethod
16 def setup_class(cls):
17 cls.G = nx.barbell_graph(4, 6)
18
19 def test_draw(self):
20 try:
21 functions = [
22 nx.draw_circular,
23 nx.draw_kamada_kawai,
24 nx.draw_planar,
25 nx.draw_random,
26 nx.draw_spectral,
27 nx.draw_spring,
28 nx.draw_shell,
29 ]
30 options = [{"node_color": "black", "node_size": 100, "width": 3}]
31 for function, option in itertools.product(functions, options):
32 function(self.G, **option)
33 plt.savefig("test.ps")
34
35 finally:
36 try:
37 os.unlink("test.ps")
38 except OSError:
39 pass
40
41 def test_draw_shell_nlist(self):
42 try:
43 nlist = [list(range(4)), list(range(4, 10)), list(range(10, 14))]
44 nx.draw_shell(self.G, nlist=nlist)
45 plt.savefig("test.ps")
46 finally:
47 try:
48 os.unlink("test.ps")
49 except OSError:
50 pass
51
52 def test_edge_colormap(self):
53 colors = range(self.G.number_of_edges())
54 nx.draw_spring(
55 self.G, edge_color=colors, width=4, edge_cmap=plt.cm.Blues, with_labels=True
56 )
57 # plt.show()
58
59 def test_arrows(self):
60 nx.draw_spring(self.G.to_directed())
61 # plt.show()
62
63 def test_edge_colors_and_widths(self):
64 pos = nx.circular_layout(self.G)
65 for G in (self.G, self.G.to_directed()):
66 nx.draw_networkx_nodes(G, pos, node_color=[(1.0, 1.0, 0.2, 0.5)])
67 nx.draw_networkx_labels(G, pos)
68 # edge with default color and width
69 nx.draw_networkx_edges(
70 G, pos, edgelist=[(0, 1)], width=None, edge_color=None
71 )
72 # edges with global color strings and widths in lists
73 nx.draw_networkx_edges(
74 G, pos, edgelist=[(0, 2), (0, 3)], width=[3], edge_color=["r"]
75 )
76 # edges with color strings and widths for each edge
77 nx.draw_networkx_edges(
78 G, pos, edgelist=[(0, 2), (0, 3)], width=[1, 3], edge_color=["r", "b"]
79 )
80 # edges with fewer color strings and widths than edges
81 nx.draw_networkx_edges(
82 G,
83 pos,
84 edgelist=[(1, 2), (1, 3), (2, 3), (3, 4)],
85 width=[1, 3],
86 edge_color=["g", "m", "c"],
87 )
88 # edges with more color strings and widths than edges
89 nx.draw_networkx_edges(
90 G,
91 pos,
92 edgelist=[(3, 4)],
93 width=[1, 2, 3, 4],
94 edge_color=["r", "b", "g", "k"],
95 )
96 # with rgb tuple and 3 edges - is interpreted with cmap
97 nx.draw_networkx_edges(
98 G, pos, edgelist=[(4, 5), (5, 6), (6, 7)], edge_color=(1.0, 0.4, 0.3)
99 )
100 # with rgb tuple in list
101 nx.draw_networkx_edges(
102 G, pos, edgelist=[(7, 8), (8, 9)], edge_color=[(0.4, 1.0, 0.0)]
103 )
104 # with rgba tuple and 4 edges - is interpretted with cmap
105 nx.draw_networkx_edges(
106 G,
107 pos,
108 edgelist=[(9, 10), (10, 11), (10, 12), (10, 13)],
109 edge_color=(0.0, 1.0, 1.0, 0.5),
110 )
111 # with rgba tuple in list
112 nx.draw_networkx_edges(
113 G,
114 pos,
115 edgelist=[(9, 10), (10, 11), (10, 12), (10, 13)],
116 edge_color=[(0.0, 1.0, 1.0, 0.5)],
117 )
118 # with color string and global alpha
119 nx.draw_networkx_edges(
120 G, pos, edgelist=[(11, 12), (11, 13)], edge_color="purple", alpha=0.2
121 )
122 # with color string in a list
123 nx.draw_networkx_edges(
124 G, pos, edgelist=[(11, 12), (11, 13)], edge_color=["purple"]
125 )
126 # with single edge and hex color string
127 nx.draw_networkx_edges(G, pos, edgelist=[(12, 13)], edge_color="#1f78b4f0")
128
129 # edge_color as numeric using vmin, vmax
130 nx.draw_networkx_edges(
131 G,
132 pos,
133 edgelist=[(7, 8), (8, 9)],
134 edge_color=[0.2, 0.5],
135 edge_vmin=0.1,
136 edge_vmax=0.6,
137 )
138
139 # plt.show()
140
141 def test_labels_and_colors(self):
142 G = nx.cubical_graph()
143 pos = nx.spring_layout(G) # positions for all nodes
144 # nodes
145 nx.draw_networkx_nodes(
146 G, pos, nodelist=[0, 1, 2, 3], node_color="r", node_size=500, alpha=0.75
147 )
148 nx.draw_networkx_nodes(
149 G,
150 pos,
151 nodelist=[4, 5, 6, 7],
152 node_color="b",
153 node_size=500,
154 alpha=[0.25, 0.5, 0.75, 1.0],
155 )
156 # edges
157 nx.draw_networkx_edges(G, pos, width=1.0, alpha=0.5)
158 nx.draw_networkx_edges(
159 G,
160 pos,
161 edgelist=[(0, 1), (1, 2), (2, 3), (3, 0)],
162 width=8,
163 alpha=0.5,
164 edge_color="r",
165 )
166 nx.draw_networkx_edges(
167 G,
168 pos,
169 edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],
170 width=8,
171 alpha=0.5,
172 edge_color="b",
173 )
174 nx.draw_networkx_edges(
175 G,
176 pos,
177 edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)],
178 min_source_margin=0.5,
179 min_target_margin=0.75,
180 width=8,
181 edge_color="b",
182 )
183 # some math labels
184 labels = {}
185 labels[0] = r"$a$"
186 labels[1] = r"$b$"
187 labels[2] = r"$c$"
188 labels[3] = r"$d$"
189 labels[4] = r"$\alpha$"
190 labels[5] = r"$\beta$"
191 labels[6] = r"$\gamma$"
192 labels[7] = r"$\delta$"
193 nx.draw_networkx_labels(G, pos, labels, font_size=16)
194 nx.draw_networkx_edge_labels(G, pos, edge_labels=None, rotate=False)
195 nx.draw_networkx_edge_labels(G, pos, edge_labels={(4, 5): "4-5"})
196 # plt.show()
197
198 def test_axes(self):
199 fig, ax = plt.subplots()
200 nx.draw(self.G, ax=ax)
201
202 def test_empty_graph(self):
203 G = nx.Graph()
204 nx.draw(G)
205
206 def test_draw_empty_nodes_return_values(self):
207 # See Issue #3833
208 from matplotlib.collections import PathCollection, LineCollection
209
210 G = nx.Graph([(1, 2), (2, 3)])
211 DG = nx.DiGraph([(1, 2), (2, 3)])
212 pos = nx.circular_layout(G)
213 assert isinstance(nx.draw_networkx_nodes(G, pos, nodelist=[]), PathCollection)
214 assert isinstance(nx.draw_networkx_nodes(DG, pos, nodelist=[]), PathCollection)
215
216 # drawing empty edges either return an empty LineCollection or empty list.
217 assert isinstance(
218 nx.draw_networkx_edges(G, pos, edgelist=[], arrows=True), LineCollection
219 )
220 assert isinstance(
221 nx.draw_networkx_edges(G, pos, edgelist=[], arrows=False), LineCollection
222 )
223 assert isinstance(
224 nx.draw_networkx_edges(DG, pos, edgelist=[], arrows=False), LineCollection
225 )
226 assert nx.draw_networkx_edges(DG, pos, edgelist=[], arrows=True) == []
227
228 def test_multigraph_edgelist_tuples(self):
229 # See Issue #3295
230 G = nx.path_graph(3, create_using=nx.MultiDiGraph)
231 nx.draw_networkx(G, edgelist=[(0, 1, 0)])
232 nx.draw_networkx(G, edgelist=[(0, 1, 0)], node_size=[10, 20, 0])
233
234 def test_alpha_iter(self):
235 pos = nx.random_layout(self.G)
236 # with fewer alpha elements than nodes
237 plt.subplot(131)
238 nx.draw_networkx_nodes(self.G, pos, alpha=[0.1, 0.2])
239 # with equal alpha elements and nodes
240 num_nodes = len(self.G.nodes)
241 alpha = [x / num_nodes for x in range(num_nodes)]
242 colors = range(num_nodes)
243 plt.subplot(132)
244 nx.draw_networkx_nodes(self.G, pos, node_color=colors, alpha=alpha)
245 # with more alpha elements than nodes
246 alpha.append(1)
247 plt.subplot(133)
248 nx.draw_networkx_nodes(self.G, pos, alpha=alpha)
249
250 def test_error_invalid_kwds(self):
251 with pytest.raises(ValueError, match="Received invalid argument"):
252 nx.draw(self.G, foo="bar")
253
254 def test_np_edgelist(self):
255 # see issue #4129
256 np = pytest.importorskip("numpy")
257 nx.draw_networkx(self.G, edgelist=np.array([(0, 2), (0, 3)]))