|
15 | 15 | import matplotlib.units |
16 | 16 | import matplotlib.testing |
17 | 17 | from matplotlib import _pylab_helpers, cbook, ft2font, pyplot as plt, ticker |
| 18 | +from matplotlib.figure import Figure |
18 | 19 | from .compare import comparable_formats, compare_images, make_test_filename |
19 | 20 | from .exceptions import ImageComparisonFailure |
20 | 21 |
|
@@ -410,27 +411,21 @@ def wrapper(*args, ext, request, **kwargs): |
410 | 411 |
|
411 | 412 | file_name = "".join(c for c in request.node.name |
412 | 413 | 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 | + ) |
434 | 429 |
|
435 | 430 | parameters = [ |
436 | 431 | param |
|
0 commit comments