code: def makefig(code, code_path, output_dir, output_base, config):
      """
      Run a pyplot script *code* and save the images under *output_dir*
      with file names derived from *output_base*
  
      """
  
      # -- Parse format list
      default_dpi = {"png": 80, "hires.png": 200, "pdf": 50}
      formats = []
      for fmt in config.plot_formats:
          if isinstance(fmt, str):
              formats.append((fmt, default_dpi.get(fmt, 80)))
          elif type(fmt) in (tuple, list) and len(fmt) == 2:
              formats.append((str(fmt[0]), int(fmt[1])))
          else:
              raise PlotError(
                  'invalid image format "%r" in plot_formats' % fmt
              )
  
      # -- Try to determine if all images already exist
  
      code_pieces = split_code_at_show(code)
  
      # Look for single-figure output files first
      all_exists = True
      img = ImageFile(output_base, output_dir)
      for format, dpi in formats:
          if out_of_date(code_path, img.filename(format)):
              all_exists = False
              break
          img.formats.append(format)
  
      if all_exists:
          return [(code, [img])]
  
      # Then look for multi-figure output files
      results = []
      all_exists = True
      for i, code_piece in enumerate(code_pieces):
          images = []
          for j in range(1000):
              img = ImageFile(
                  "%s_%02d_%02d" % (output_base, i, j), output_dir
              )
              for format, dpi in formats:
                  if out_of_date(code_path, img.filename(format)):
                      all_exists = False
                      break
                  img.formats.append(format)
  
              # assume that if we have one, we have them all
              if not all_exists:
                  all_exists = j > 0
                  break
              images.append(img)
          if not all_exists:
              break
          results.append((code_piece, images))
  
      if all_exists:
          return results
  
      # -- We didn't find the files, so build them
  
      results = []
      ns = {}
  
      for i, code_piece in enumerate(code_pieces):
          # Clear between runs
          plt.close("all")
  
          # Run code
          run_code(code_piece, code_path, ns)
  
          # Collect images
          images = []
          fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()
          for j, figman in enumerate(fig_managers):
              if len(fig_managers) == 1 and len(code_pieces) == 1:
                  img = ImageFile(output_base, output_dir)
              else:
                  img = ImageFile(
                      "%s_%02d_%02d" % (output_base, i, j), output_dir
                  )
              images.append(img)
              for format, dpi in formats:
                  try:
                      figman.canvas.figure.savefig(
                          img.filename(format), dpi=dpi
                      )
                  except exceptions.BaseException as err:
                      raise PlotError(traceback.format_exc())
                  img.formats.append(format)
  
          # Results
          results.append((code_piece, images))
  
      return results
  
imports: from __future__ import division, absolute_import, print_function
  import sys, os, glob, shutil, imp, warnings, re, textwrap, traceback
  import sphinx
  import warnings
  from docutils.parsers.rst import directives
  from docutils import nodes
  from docutils import nodes, utils
  import matplotlib
  import matplotlib.pyplot
  import matplotlib.image
  from matplotlib import _pylab_helpers
  import exceptions
label_desc: Use image node_id 12 for calling a global function with example usage: makefig(code, code_path, output_dir, output_base, config) and returns: results, list, results