code: def render_figures(
      code,
      code_path,
      output_dir,
      output_base,
      context,
      function_name,
      config,
      context_reset=False,
      close_figs=False,
  ):
      """
      Run a pyplot script and save the images in *output_dir*.
  
      Save the images under *output_dir* with file names derived from
      *output_base*
      """
      formats = get_plot_formats(config)
  
      # -- 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 xrange(1000):
              if len(code_pieces) > 1:
                  img = ImageFile(
                      "%s_%02d_%02d" % (output_base, i, j), output_dir
                  )
              else:
                  img = ImageFile(
                      "%s_%02d" % (output_base, 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 = []
      if context:
          ns = plot_context
      else:
          ns = {}
  
      if context_reset:
          clear_state(config.plot_rcparams)
          plot_context.clear()
  
      close_figs = not context or close_figs
  
      for i, code_piece in enumerate(code_pieces):
          if not context or config.plot_apply_rcparams:
              clear_state(config.plot_rcparams, close_figs)
          elif close_figs:
              plt.close("all")
  
          run_code(code_piece, code_path, ns, function_name)
  
          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)
              elif len(code_pieces) == 1:
                  img = ImageFile(
                      "%s_%02d" % (output_base, j), 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 Exception as err:
                      raise PlotError(traceback.format_exc())
                  img.formats.append(format)
  
          results.append((code_piece, images))
  
      if not context or config.plot_apply_rcparams:
          clear_state(config.plot_rcparams, close=not context)
  
      return results
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import six
  from six.moves import xrange
  import sys, os, shutil, io, re, textwrap
  from os.path import relpath
  import traceback
  import warnings
  from docutils.parsers.rst import directives
  from docutils.parsers.rst.directives.images import Image
  import sphinx
  import jinja2
  import matplotlib
  import matplotlib.cbook
  from matplotlib import _pylab_helpers
label_desc: Use image node_id 16 for calling a global function with example usage: render_figures(code, code_path, output_dir, output_base, context, function_name, config, context_reset, close_figs) and returns: results, list, results