code: def render_figures(
      code,
      code_path,
      output_dir,
      output_base,
      context,
      function_name,
      config,
      context_reset=False,
      close_figs=False,
      code_includes=None,
  ):
      """
      Run a pyplot script and save the images in *output_dir*.
  
      Save the images under *output_dir* with file names derived from
      *output_base*
      """
  
      if function_name is not None:
          output_base = f"{output_base}_{function_name}"
      formats = get_plot_formats(config)
  
      # Try to determine if all images already exist
  
      is_doctest, code_pieces = _split_code_at_show(code, function_name)
      # Look for single-figure output files first
      img = ImageFile(output_base, output_dir)
      for format, dpi in formats:
          if context or out_of_date(
              code_path, img.filename(format), includes=code_includes
          ):
              all_exists = False
              break
          img.formats.append(format)
      else:
          all_exists = True
  
      if all_exists:
          return [(code, [img])]
  
      # Then look for multi-figure output files
      results = []
      for i, code_piece in enumerate(code_pieces):
          images = []
          for j in itertools.count():
              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 fmt, dpi in formats:
                  if context or out_of_date(
                      code_path,
                      img.filename(fmt),
                      includes=code_includes,
                  ):
                      all_exists = False
                      break
                  img.formats.append(fmt)
  
              # 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))
      else:
          all_exists = True
  
      if all_exists:
          return results
  
      # We didn't find the files, so build them
  
      results = []
      ns = plot_context if context else {}
  
      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(
              doctest.script_from_examples(code_piece)
              if is_doctest
              else 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 fmt, dpi in formats:
                  try:
                      figman.canvas.figure.savefig(
                          img.filename(fmt), dpi=dpi
                      )
                      if fmt == formats[0][0] and config.plot_srcset:
                          # save a 2x, 3x etc version of the default...
                          srcset = _parse_srcset(config.plot_srcset)
                          for mult, suffix in srcset.items():
                              fm = f"{suffix}.{fmt}"
                              img.formats.append(fm)
                              figman.canvas.figure.savefig(
                                  img.filename(fm), dpi=int(dpi * mult)
                              )
                  except Exception as err:
                      raise PlotError(traceback.format_exc()) from err
                  img.formats.append(fmt)
  
          results.append((code_piece, images))
  
      if not context or config.plot_apply_rcparams:
          clear_state(config.plot_rcparams, close=not context)
  
      return results
  
imports: import contextlib
  import doctest
  from io import StringIO
  import itertools
  import os
  from os.path import relpath
  from pathlib import Path
  import re
  import shutil
  import sys
  import textwrap
  import traceback
  from docutils.parsers.rst import directives, Directive
  from docutils.parsers.rst.directives.images import Image
  import jinja2
  from sphinx.errors import ExtensionError
  import matplotlib
  from matplotlib.backend_bases import FigureManagerBase
  import matplotlib.pyplot
  from matplotlib import _pylab_helpers, cbook
label_desc: Use image node_id 14 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, code_includes) and returns: results, list, results