forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pyplot.py
More file actions
322 lines (264 loc) · 8.79 KB
/
test_pyplot.py
File metadata and controls
322 lines (264 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import difflib
import subprocess
import sys
from pathlib import Path
import pytest
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.cbook import MatplotlibDeprecationWarning
def test_pyplot_up_to_date(tmpdir):
gen_script = Path(mpl.__file__).parents[2] / "tools/boilerplate.py"
if not gen_script.exists():
pytest.skip("boilerplate.py not found")
orig_contents = Path(plt.__file__).read_text()
plt_file = tmpdir.join('pyplot.py')
plt_file.write_text(orig_contents, 'utf-8')
subprocess.run([sys.executable, str(gen_script), str(plt_file)],
check=True)
new_contents = plt_file.read_text('utf-8')
if orig_contents != new_contents:
diff_msg = '\n'.join(
difflib.unified_diff(
orig_contents.split('\n'), new_contents.split('\n'),
fromfile='found pyplot.py',
tofile='expected pyplot.py',
n=0, lineterm=''))
pytest.fail(
"pyplot.py is not up-to-date. Please run "
"'python tools/boilerplate.py' to update pyplot.py. "
"This needs to be done from an environment where your "
"current working copy is installed (e.g. 'pip install -e'd). "
"Here is a diff of unexpected differences:\n%s" % diff_msg
)
def test_copy_docstring_and_deprecators(recwarn):
@mpl._api.rename_parameter("(version)", "old", "new")
@mpl._api.make_keyword_only("(version)", "kwo")
def func(new, kwo=None):
pass
@plt._copy_docstring_and_deprecators(func)
def wrapper_func(new, kwo=None):
pass
wrapper_func(None)
wrapper_func(new=None)
wrapper_func(None, kwo=None)
wrapper_func(new=None, kwo=None)
assert not recwarn
with pytest.warns(MatplotlibDeprecationWarning):
wrapper_func(old=None)
with pytest.warns(MatplotlibDeprecationWarning):
wrapper_func(None, None)
def test_pyplot_box():
fig, ax = plt.subplots()
plt.box(False)
assert not ax.get_frame_on()
plt.box(True)
assert ax.get_frame_on()
plt.box()
assert not ax.get_frame_on()
plt.box()
assert ax.get_frame_on()
def test_stackplot_smoke():
# Small smoke test for stackplot (see #12405)
plt.stackplot([1, 2, 3], [1, 2, 3])
def test_nrows_error():
with pytest.raises(TypeError):
plt.subplot(nrows=1)
with pytest.raises(TypeError):
plt.subplot(ncols=1)
def test_ioff():
plt.ion()
assert mpl.is_interactive()
with plt.ioff():
assert not mpl.is_interactive()
assert mpl.is_interactive()
plt.ioff()
assert not mpl.is_interactive()
with plt.ioff():
assert not mpl.is_interactive()
assert not mpl.is_interactive()
def test_ion():
plt.ioff()
assert not mpl.is_interactive()
with plt.ion():
assert mpl.is_interactive()
assert not mpl.is_interactive()
plt.ion()
assert mpl.is_interactive()
with plt.ion():
assert mpl.is_interactive()
assert mpl.is_interactive()
def test_nested_ion_ioff():
# initial state is interactive
plt.ion()
# mixed ioff/ion
with plt.ioff():
assert not mpl.is_interactive()
with plt.ion():
assert mpl.is_interactive()
assert not mpl.is_interactive()
assert mpl.is_interactive()
# redundant contexts
with plt.ioff():
with plt.ioff():
assert not mpl.is_interactive()
assert mpl.is_interactive()
with plt.ion():
plt.ioff()
assert mpl.is_interactive()
# initial state is not interactive
plt.ioff()
# mixed ioff/ion
with plt.ion():
assert mpl.is_interactive()
with plt.ioff():
assert not mpl.is_interactive()
assert mpl.is_interactive()
assert not mpl.is_interactive()
# redundant contexts
with plt.ion():
with plt.ion():
assert mpl.is_interactive()
assert not mpl.is_interactive()
with plt.ioff():
plt.ion()
assert not mpl.is_interactive()
def test_close():
try:
plt.close(1.1)
except TypeError as e:
assert str(e) == "close() argument must be a Figure, an int, " \
"a string, or None, not <class 'float'>"
def test_subplot_reuse():
ax1 = plt.subplot(121)
assert ax1 is plt.gca()
ax2 = plt.subplot(122)
assert ax2 is plt.gca()
ax3 = plt.subplot(121)
assert ax1 is plt.gca()
assert ax1 is ax3
def test_axes_kwargs():
# plt.axes() always creates new axes, even if axes kwargs differ.
plt.figure()
ax = plt.axes()
ax1 = plt.axes()
assert ax is not None
assert ax1 is not ax
plt.close()
plt.figure()
ax = plt.axes(projection='polar')
ax1 = plt.axes(projection='polar')
assert ax is not None
assert ax1 is not ax
plt.close()
plt.figure()
ax = plt.axes(projection='polar')
ax1 = plt.axes()
assert ax is not None
assert ax1.name == 'rectilinear'
assert ax1 is not ax
plt.close()
def test_subplot_replace_projection():
# plt.subplot() searches for axes with the same subplot spec, and if one
# exists, and the kwargs match returns it, create a new one if they do not
fig = plt.figure()
ax = plt.subplot(1, 2, 1)
ax1 = plt.subplot(1, 2, 1)
ax2 = plt.subplot(1, 2, 2)
# This will delete ax / ax1 as they fully overlap
ax3 = plt.subplot(1, 2, 1, projection='polar')
ax4 = plt.subplot(1, 2, 1, projection='polar')
assert ax is not None
assert ax1 is ax
assert ax2 is not ax
assert ax3 is not ax
assert ax3 is ax4
assert ax not in fig.axes
assert ax2 in fig.axes
assert ax3 in fig.axes
assert ax.name == 'rectilinear'
assert ax2.name == 'rectilinear'
assert ax3.name == 'polar'
def test_subplot_kwarg_collision():
ax1 = plt.subplot(projection='polar', theta_offset=0)
ax2 = plt.subplot(projection='polar', theta_offset=0)
assert ax1 is ax2
ax3 = plt.subplot(projection='polar', theta_offset=1)
assert ax1 is not ax3
assert ax1 not in plt.gcf().axes
def test_gca_kwargs():
# plt.gca() returns an existing axes, unless there were no axes.
plt.figure()
ax = plt.gca()
ax1 = plt.gca()
assert ax is not None
assert ax1 is ax
plt.close()
# plt.gca() raises a DeprecationWarning if called with kwargs.
plt.figure()
with pytest.warns(
MatplotlibDeprecationWarning,
match=r'Calling gca\(\) with keyword arguments was deprecated'):
ax = plt.gca(projection='polar')
ax1 = plt.gca()
assert ax is not None
assert ax1 is ax
assert ax1.name == 'polar'
plt.close()
# plt.gca() ignores keyword arguments if an axes already exists.
plt.figure()
ax = plt.gca()
with pytest.warns(
MatplotlibDeprecationWarning,
match=r'Calling gca\(\) with keyword arguments was deprecated'):
ax1 = plt.gca(projection='polar')
assert ax is not None
assert ax1 is ax
assert ax1.name == 'rectilinear'
plt.close()
def test_subplot_projection_reuse():
# create an axes
ax1 = plt.subplot(111)
# check that it is current
assert ax1 is plt.gca()
# make sure we get it back if we ask again
assert ax1 is plt.subplot(111)
# create a polar plot
ax2 = plt.subplot(111, projection='polar')
assert ax2 is plt.gca()
# this should have deleted the first axes
assert ax1 not in plt.gcf().axes
# assert we get it back if no extra parameters passed
assert ax2 is plt.subplot(111)
# now check explicitly setting the projection to rectilinear
# makes a new axes
ax3 = plt.subplot(111, projection='rectilinear')
assert ax3 is plt.gca()
assert ax3 is not ax2
assert ax2 not in plt.gcf().axes
def test_subplot_polar_normalization():
ax1 = plt.subplot(111, projection='polar')
ax2 = plt.subplot(111, polar=True)
ax3 = plt.subplot(111, polar=True, projection='polar')
assert ax1 is ax2
assert ax1 is ax3
with pytest.raises(ValueError,
match="polar=True, yet projection='3d'"):
ax2 = plt.subplot(111, polar=True, projection='3d')
def test_subplot_change_projection():
ax = plt.subplot()
projections = ('aitoff', 'hammer', 'lambert', 'mollweide',
'polar', 'rectilinear', '3d')
for proj in projections:
ax_next = plt.subplot(projection=proj)
assert ax_next is plt.subplot()
assert ax_next.name == proj
assert ax is not ax_next
ax = ax_next
def test_polar_second_call():
# the first call creates the axes with polar projection
ln1, = plt.polar(0., 1., 'ro')
assert isinstance(ln1, mpl.lines.Line2D)
# the second call should reuse the existing axes
ln2, = plt.polar(1.57, .5, 'bo')
assert isinstance(ln2, mpl.lines.Line2D)
assert ln1.axes is ln2.axes