Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c224d48

Browse files
authored
Avoid using pyplot for check_figures_equal (#31076)
* Avoid using pyplot for check_figures_equal * Fix tests * Add a check for both figures being empty * Fix tests for empty-figure check * Add suggested comment * Revise comment about cairo backend
1 parent 67e56a2 commit c224d48

6 files changed

Lines changed: 36 additions & 39 deletions

File tree

lib/matplotlib/testing/decorators.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import matplotlib.units
1616
import matplotlib.testing
1717
from matplotlib import _pylab_helpers, cbook, ft2font, pyplot as plt, ticker
18+
from matplotlib.figure import Figure
1819
from .compare import comparable_formats, compare_images, make_test_filename
1920
from .exceptions import ImageComparisonFailure
2021

@@ -410,27 +411,21 @@ def wrapper(*args, ext, request, **kwargs):
410411

411412
file_name = "".join(c for c in request.node.name
412413
if c in ALLOWED_CHARS)
413-
try:
414-
fig_test = plt.figure("test")
415-
fig_ref = plt.figure("reference")
416-
with _collect_new_figures() as figs:
417-
func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs)
418-
if figs:
419-
raise RuntimeError('Number of open figures changed during '
420-
'test. Make sure you are plotting to '
421-
'fig_test or fig_ref, or if this is '
422-
'deliberate explicitly close the '
423-
'new figure(s) inside the test.')
424-
test_image_path = result_dir / (file_name + "." + ext)
425-
ref_image_path = result_dir / (file_name + "-expected." + ext)
426-
fig_test.savefig(test_image_path)
427-
fig_ref.savefig(ref_image_path)
428-
_raise_on_image_difference(
429-
ref_image_path, test_image_path, tol=tol
430-
)
431-
finally:
432-
plt.close(fig_test)
433-
plt.close(fig_ref)
414+
fig_test = Figure()
415+
fig_ref = Figure()
416+
func(*args, fig_test=fig_test, fig_ref=fig_ref, **kwargs)
417+
if len(fig_test.get_children()) == 1 and len(fig_ref.get_children()) == 1:
418+
# no artists have been added. The only child is fig.patch.
419+
raise RuntimeError("Both figures are empty. Make sure you are "
420+
"plotting to fig_test or fig_ref.")
421+
422+
test_image_path = result_dir / (file_name + "." + ext)
423+
ref_image_path = result_dir / (file_name + "-expected." + ext)
424+
fig_test.savefig(test_image_path)
425+
fig_ref.savefig(ref_image_path)
426+
_raise_on_image_difference(
427+
ref_image_path, test_image_path, tol=tol
428+
)
434429

435430
parameters = [
436431
param

lib/matplotlib/tests/test_backend_cairo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
import matplotlib.pyplot as plt
56
from matplotlib.testing.decorators import check_figures_equal
67
from matplotlib import (
78
collections as mcollections, patches as mpatches, path as mpath)
@@ -46,3 +47,7 @@ def test_patch_alpha_coloring(fig_test, fig_ref):
4647
facecolor=(1, 0, 0, 0.5),
4748
edgecolor=(0, 0, 1, 0.75))
4849
ax.add_collection(col)
50+
51+
# Have pyplot manage the figures to ensure the cairo backend is used
52+
plt.figure(fig_ref)
53+
plt.figure(fig_test)

lib/matplotlib/tests/test_colorbar.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,12 @@ def test_twoslope_colorbar():
895895
fig.colorbar(pc)
896896

897897

898-
@check_figures_equal()
899-
def test_remove_cb_whose_mappable_has_no_figure(fig_ref, fig_test):
900-
ax = fig_test.add_subplot()
901-
cb = fig_test.colorbar(cm.ScalarMappable(), cax=ax)
898+
def test_remove_cb_whose_mappable_has_no_figure():
899+
fig, ax = plt.subplots()
900+
assert fig.get_axes() != []
901+
cb = fig.colorbar(cm.ScalarMappable(), cax=ax)
902902
cb.remove()
903+
assert fig.get_axes() == []
903904

904905

905906
def test_aspects():

lib/matplotlib/tests/test_image.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,9 @@ def test_large_image(fig_test, fig_ref, dim, size, msg, origin, high_memory):
16021602
with pytest.warns(UserWarning,
16031603
match=f'Data with more than {msg} cannot be '
16041604
'accurately displayed.'):
1605-
fig_test.canvas.draw()
1605+
with io.BytesIO() as buffer:
1606+
# Write to a buffer to trigger the warning
1607+
fig_test.savefig(buffer)
16061608

16071609
array = np.zeros((1, size // 2 + 1))
16081610
array[:, array.size // 2:] = 1

lib/matplotlib/tests/test_polar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_polar_units_1(fig_test, fig_ref):
117117
xs = [30.0, 45.0, 60.0, 90.0]
118118
ys = [1.0, 2.0, 3.0, 4.0]
119119

120-
plt.figure(fig_test.number)
120+
plt.figure(fig_test)
121121
plt.polar([x * units.deg for x in xs], ys)
122122

123123
ax = fig_ref.add_subplot(projection="polar")
@@ -134,7 +134,7 @@ def test_polar_units_2(fig_test, fig_ref):
134134
ys = [1.0, 2.0, 3.0, 4.0]
135135
ys_km = [y * units.km for y in ys]
136136

137-
plt.figure(fig_test.number)
137+
plt.figure(fig_test)
138138
# test {theta,r}units.
139139
plt.polar(xs_deg, ys_km, thetaunits="rad", runits="km")
140140
assert isinstance(plt.gca().xaxis.get_major_formatter(),

lib/matplotlib/tests/test_testing.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44

5-
import matplotlib.pyplot as plt
65
from matplotlib.testing.decorators import check_figures_equal
76

87

@@ -17,6 +16,8 @@ def test_warn_to_fail():
1716
@check_figures_equal()
1817
@pytest.mark.parametrize("b", [1])
1918
def test_parametrize_with_check_figure_equal(a, fig_ref, b, fig_test):
19+
fig_ref.add_subplot()
20+
fig_test.add_subplot()
2021
assert a == b
2122

2223

@@ -28,14 +29,7 @@ def should_fail(test, ref):
2829

2930

3031
@pytest.mark.xfail(raises=RuntimeError, strict=True,
31-
reason='Test for check_figures_equal test creating '
32-
'new figures')
32+
reason="Both figures are empty")
3333
@check_figures_equal()
34-
def test_check_figures_equal_extra_fig(fig_test, fig_ref):
35-
plt.figure()
36-
37-
38-
@check_figures_equal()
39-
def test_check_figures_equal_closed_fig(fig_test, fig_ref):
40-
fig = plt.figure()
41-
plt.close(fig)
34+
def test_check_figures_equal_empty_figs(fig_test, fig_ref):
35+
pass

0 commit comments

Comments
 (0)