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
510 lines (406 loc) · 14.2 KB
/
test_pyplot.py
File metadata and controls
510 lines (406 loc) · 14.2 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import difflib
import inspect
import numpy as np
import sys
from pathlib import Path
import pytest
import matplotlib as mpl
from matplotlib.testing import subprocess_run_for_testing
from matplotlib import pyplot as plt
def test_pyplot_up_to_date(tmp_path):
pytest.importorskip("black")
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 = tmp_path / 'pyplot.py'
plt_file.write_text(orig_contents, 'utf-8')
subprocess_run_for_testing(
[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(mpl.__version__, "old", "new")
@mpl._api.make_keyword_only(mpl.__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(mpl.MatplotlibDeprecationWarning):
wrapper_func(old=None)
with pytest.warns(mpl.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) == (
"'fig' must be an instance of matplotlib.figure.Figure, int, str "
"or None, not a 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)
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 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
ax1.remove()
ax3 = plt.subplot(projection='polar', theta_offset=1)
assert ax1 is not ax3
assert ax1 not in plt.gcf().axes
def test_gca():
# 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()
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)
# remove it
ax1.remove()
# 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)
ax2.remove()
# 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():
created_axes = set()
ax = plt.subplot()
created_axes.add(ax)
projections = ('aitoff', 'hammer', 'lambert', 'mollweide',
'polar', 'rectilinear', '3d')
for proj in projections:
ax.remove()
ax = plt.subplot(projection=proj)
assert ax is plt.subplot()
assert ax.name == proj
created_axes.add(ax)
# Check that each call created a new Axes.
assert len(created_axes) == 1 + len(projections)
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
def test_fallback_position():
# check that position kwarg works if rect not supplied
axref = plt.axes([0.2, 0.2, 0.5, 0.5])
axtest = plt.axes(position=[0.2, 0.2, 0.5, 0.5])
np.testing.assert_allclose(axtest.bbox.get_points(),
axref.bbox.get_points())
# check that position kwarg ignored if rect is supplied
axref = plt.axes([0.2, 0.2, 0.5, 0.5])
axtest = plt.axes([0.2, 0.2, 0.5, 0.5], position=[0.1, 0.1, 0.8, 0.8])
np.testing.assert_allclose(axtest.bbox.get_points(),
axref.bbox.get_points())
def test_set_current_figure_via_subfigure():
fig1 = plt.figure()
subfigs = fig1.subfigures(2)
plt.figure()
assert plt.gcf() != fig1
current = plt.figure(subfigs[1])
assert plt.gcf() == fig1
assert current == fig1
def test_set_current_axes_on_subfigure():
fig = plt.figure()
subfigs = fig.subfigures(2)
ax = subfigs[0].subplots(1, squeeze=True)
subfigs[1].subplots(1, squeeze=True)
assert plt.gca() != ax
plt.sca(ax)
assert plt.gca() == ax
def test_pylab_integration():
IPython = pytest.importorskip("IPython")
mpl.testing.subprocess_run_helper(
IPython.start_ipython,
"--pylab",
"-c",
";".join((
"import matplotlib.pyplot as plt",
"assert plt._REPL_DISPLAYHOOK == plt._ReplDisplayHook.IPYTHON",
)),
timeout=60,
)
def test_doc_pyplot_summary():
"""Test that pyplot_summary lists all the plot functions."""
pyplot_docs = Path(__file__).parent / '../../../doc/api/pyplot_summary.rst'
if not pyplot_docs.exists():
pytest.skip("Documentation sources not available")
def extract_documented_functions(lines):
"""
Return a list of all the functions that are mentioned in the
autosummary blocks contained in *lines*.
An autosummary block looks like this::
.. autosummary::
:toctree: _as_gen
:template: autosummary.rst
:nosignatures:
plot
errorbar
"""
functions = []
in_autosummary = False
for line in lines:
if not in_autosummary:
if line.startswith(".. autosummary::"):
in_autosummary = True
else:
if not line or line.startswith(" :"):
# empty line or autosummary parameter
continue
if not line[0].isspace():
# no more indentation: end of autosummary block
in_autosummary = False
continue
functions.append(line.strip())
return functions
lines = pyplot_docs.read_text().split("\n")
doc_functions = set(extract_documented_functions(lines))
plot_commands = set(plt._get_pyplot_commands())
missing = plot_commands.difference(doc_functions)
if missing:
raise AssertionError(
f"The following pyplot functions are not listed in the "
f"documentation. Please add them to doc/api/pyplot_summary.rst: "
f"{missing!r}")
extra = doc_functions.difference(plot_commands)
if extra:
raise AssertionError(
f"The following functions are listed in the pyplot documentation, "
f"but they do not exist in pyplot. "
f"Please remove them from doc/api/pyplot_summary.rst: {extra!r}")
def test_minor_ticks():
plt.figure()
plt.plot(np.arange(1, 10))
tick_pos, tick_labels = plt.xticks(minor=True)
assert np.all(tick_labels == np.array([], dtype=np.float64))
assert tick_labels == []
plt.yticks(ticks=[3.5, 6.5], labels=["a", "b"], minor=True)
ax = plt.gca()
tick_pos = ax.get_yticks(minor=True)
tick_labels = ax.get_yticklabels(minor=True)
assert np.all(tick_pos == np.array([3.5, 6.5]))
assert [l.get_text() for l in tick_labels] == ['a', 'b']
def test_switch_backend_no_close():
plt.switch_backend('agg')
fig = plt.figure()
fig = plt.figure()
assert len(plt.get_fignums()) == 2
plt.switch_backend('agg')
assert len(plt.get_fignums()) == 2
plt.switch_backend('svg')
assert len(plt.get_fignums()) == 2
def figure_hook_example(figure):
figure._test_was_here = True
def test_figure_hook():
test_rc = {
'figure.hooks': ['matplotlib.tests.test_pyplot:figure_hook_example']
}
with mpl.rc_context(test_rc):
fig = plt.figure()
assert fig._test_was_here
def test_multiple_same_figure_calls():
fig = plt.figure(1, figsize=(1, 2))
with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
fig2 = plt.figure(1, figsize=np.array([3, 4]))
with pytest.warns(UserWarning, match="Ignoring specified arguments in this call"):
plt.figure(fig, figsize=np.array([5, 6]))
assert fig is fig2
fig3 = plt.figure(1) # Checks for false warnings
assert fig is fig3
def test_close_all_warning():
fig1 = plt.figure()
# Check that the warning is issued when 'all' is passed to plt.figure
with pytest.warns(UserWarning, match="closes all existing figures"):
fig2 = plt.figure("all")
def test_matshow():
fig = plt.figure()
arr = [[0, 1], [1, 2]]
# Smoke test that matshow does not ask for a new figsize on the existing figure
plt.matshow(arr, fignum=fig.number)
def assert_same_signature(func1, func2):
"""
Assert that `func1` and `func2` have the same arguments,
i.e. same parameter count, names and kinds.
:param func1: First function to check
:param func2: Second function to check
"""
params1 = inspect.signature(func1).parameters
params2 = inspect.signature(func2).parameters
assert len(params1) == len(params2)
assert all([
params1[p].name == params2[p].name and
params1[p].kind == params2[p].kind
for p in params1
])
def test_setloglevel_signature():
assert_same_signature(plt.set_loglevel, mpl.set_loglevel)