code: def _import_matplotlib():
      
      # make sure that the Agg backend is set before importing any
      # matplotlib
      import matplotlib
      matplotlib.use('agg')
      matplotlib_backend = matplotlib.get_backend().lower()
  
      if matplotlib_backend != 'agg':
          raise ValueError(
              "Sphinx-Gallery relies on the matplotlib 'agg' backend to "
              "render figures and write them to files. You are "
              "currently using the {} backend. Sphinx-Gallery will "
              "terminate the build now, because changing backends is "
              "not well supported by matplotlib. We advise you to move "
              "sphinx_gallery imports before any matplotlib-dependent "
              "import. Moving sphinx_gallery imports at the top of "
              "your conf.py file should fix this issue"
              .format(matplotlib_backend))
  
      import matplotlib.pyplot as plt
      return matplotlib, plt
docs: Import matplotlib safely.