matplotlib/pytest-mpl

Fix `MatplotlibDeprecationWarning` by closing figures before backend switching

bersbersbers opened this issue ยท 4 comments

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.

Thanks @bersbersbers for reporting this, we address this deprecation warning ๐Ÿ‘

@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.

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

@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.