matplotlib / pytest-mpl

A pytest plugin to facilitate image comparison for Matplotlib figures
Other
244 stars 47 forks source link

Fix `MatplotlibDeprecationWarning` by closing figures before backend switching #206

Closed bersbersbers closed 11 months ago

bersbersbers commented 11 months ago

I am getting this warning on matplotlib 3.8:

c:\...\.venv\Lib\site-packages\pytest_mpl\plugin.py:220: MatplotlibDeprecationWarning:
Auto-close()ing of figures upon backend switching is deprecated since 3.8 and will be
removed two minor releases later.  To suppress this warning, explicitly call plt.close('all') first.
bjlittle commented 11 months ago

Thanks @bersbersbers for reporting this, we address this deprecation warning 👍

dopplershift commented 11 months ago

@bersbersbers I'll note that I've been fixing a lot of these in my MetPy test suite. In all cases, these weren't actually problems with the current release of pytest-mpl, but were separate tests that were creating figures (more as "smoke tests") and weren't using the @pytest.mark.mpl_image_compare decorator. I was able to silence all of them by making sure those tests called plt.close(fig) before the test ended.

bersbersbers commented 11 months ago

@dopplershift thanks - there is a big chance you are right! I'll check that tomorrow and let you know.

bersbersbers commented 11 months ago

@dopplershift was right - I was accidentally creating a plot during test collection, which pytest-mpl could not be aware of. The warning should actually be considered helpful.