Repository: sphinx-gallery/sphinx-gallery

Issue:
Resetting matplotlib module
The default [matplotlib resetter](https://github.com/sphinx-gallery/sphinx-gallery/blob/9a09fc1377de833db9f1d7f23686e37c3d7a02dc/sphinx_gallery/scrapers.py#L545) only resets the `rcParams`; it does not reset the [units registry](https://matplotlib.org/stable/api/units_api.html#matplotlib.units.Registry).  
This can lead to side effects if, for instance, a script registers the [`ConciseDateConverter`](https://matplotlib.org/stable/api/dates_api.html#matplotlib.dates.ConciseDateConverter) for various date types. Subsequent scripts will use the `ConciseDateFormatter` for date axis objects despite that `date.converter` is set to `auto` in the default `rcParams` (happened when building the [Basic Usage tutorial](https://matplotlib.org/devdocs/tutorials/introductory/usage.html) after the [Formatting date ticks using ConciseDateFormatter example](https://matplotlib.org/stable/gallery/ticks/date_concise_formatter.html), see matplotlib/matplotlib#21800).  

While the [documentation](https://sphinx-gallery.github.io/stable/configuration.html#resetting-modules) correctly states that the default matplotlib resetter works by calling `matplotlib.pyplot.rcdefaults()` it nevertheless conveys the impression that this would completely reset matplotlib.  

So maybe the documentation should at least contain a warning that the `units.registry` is not reset by the default resetter or - even better  - should reset the registry too (there isn't, however, a single function to reset it to its default setting of  
```
{decimal.Decimal: <matplotlib.units.DecimalConverter>,
 str: <matplotlib.category.StrCategoryConverter>,
 numpy.str_: <matplotlib.category.StrCategoryConverter>,
 bytes: <matplotlib.category.StrCategoryConverter>,
 numpy.bytes_: <matplotlib.category.StrCategoryConverter>,
 numpy.datetime64: <matplotlib.dates._SwitchableDateConverter>,
 datetime.date: <matplotlib.dates._SwitchableDateConverter>,
 datetime.datetime: <matplotlib.dates._SwitchableDateConverter>}
```



Discussion:
StefRe: Hm, the failing tests [test_rebuild](https://dev.azure.com/sphinx-gallery/sphinx-gallery/_build/results?buildId=493&view=logs&j=b5bc3588-312a-5ed9-2cbc-45b5a4514230&t=75564686-7662-5116-598f-6c06898860a6&l=218) and [TestLoggingTee](https://dev.azure.com/sphinx-gallery/sphinx-gallery/_build/results?buildId=493&view=logs&j=6d3a2fd5-db06-5d36-78f6-1b134852c6a9&t=904f871b-b76f-5ede-a33e-10bacf8c1184&l=217) seem (at least to me) not be related to this PR?

Patch:
diff --git a/doc/configuration.rst b/doc/configuration.rst
index 1251d84..d4cd4de 100644
--- a/doc/configuration.rst
+++ b/doc/configuration.rst
@@ -1321,7 +1321,8 @@ to ensure that any changes made to plotting behavior in one example do not
 propagate to the other examples.
 
 By default, before each example file executes, Sphinx-Gallery will
-reset ``matplotlib`` (by using :func:`matplotlib.pyplot.rcdefaults`) and ``seaborn``
+reset ``matplotlib`` (by using :func:`matplotlib.pyplot.rcdefaults` and
+reloading submodules that populate the units registry) and ``seaborn``
 (by trying to unload the module from ``sys.modules``). This is equivalent to the
 following configuration::
 
diff --git a/sphinx_gallery/scrapers.py b/sphinx_gallery/scrapers.py
index 33eae3a..5000b52 100644
--- a/sphinx_gallery/scrapers.py
+++ b/sphinx_gallery/scrapers.py
@@ -13,6 +13,7 @@ images are injected as rst ``image-sg`` directives into the ``.rst``
 file generated for each example script.
 """
 
+import importlib
 import inspect
 import os
 import sys
@@ -544,8 +545,11 @@ SINGLE_IMAGE = """
 
 def _reset_matplotlib(gallery_conf, fname):
     """Reset matplotlib."""
-    _, plt = _import_matplotlib()
+    mpl, plt = _import_matplotlib()
     plt.rcdefaults()
+    importlib.reload(mpl.units)
+    importlib.reload(mpl.dates)
+    importlib.reload(mpl.category)
 
 
 def _reset_seaborn(gallery_conf, fname):


Test patch:
diff --git a/sphinx_gallery/tests/test_scrapers.py b/sphinx_gallery/tests/test_scrapers.py
index ef35746..485867e 100644
--- a/sphinx_gallery/tests/test_scrapers.py
+++ b/sphinx_gallery/tests/test_scrapers.py
@@ -7,7 +7,8 @@ import sphinx_gallery
 from sphinx_gallery.gen_gallery import _complete_gallery_conf
 from sphinx_gallery.scrapers import (figure_rst, mayavi_scraper, SG_IMAGE,
                                      matplotlib_scraper, ImagePathIterator,
-                                     save_figures, _KNOWN_IMG_EXTS)
+                                     save_figures, _KNOWN_IMG_EXTS,
+                                     _reset_matplotlib)
 from sphinx_gallery.utils import _get_image
 
 
@@ -313,3 +314,15 @@ def test_iterator():
     with pytest.raises(ExtensionError, match='10 images'):
         for ii in ipi:
             pass
+
+
+def test_reset_matplotlib(gallery_conf):
+    """Test _reset_matplotlib."""
+    import matplotlib
+    matplotlib.rcParams['lines.linewidth'] = 42
+    matplotlib.units.registry.clear()
+
+    _reset_matplotlib(gallery_conf, '')
+
+    assert matplotlib.rcParams['lines.linewidth'] != 42
+    assert len(matplotlib.units.registry) > 0