code: def _plot_surf_matplotlib(
      coords,
      faces,
      surf_map=None,
      bg_map=None,
      hemi="left",
      view="lateral",
      cmap=None,
      colorbar=False,
      avg_method="mean",
      threshold=None,
      alpha="auto",
      bg_on_data=False,
      darkness=0.7,
      vmin=None,
      vmax=None,
      cbar_vmin=None,
      cbar_vmax=None,
      cbar_tick_format="%.2g",
      title=None,
      title_font_size=18,
      output_file=None,
      axes=None,
      figure=None,
      **kwargs,
  ):
      """Help for plot_surf.
  
      This function handles surface plotting when the selected
      engine is matplotlib.
      """
      _default_figsize = [4, 5]
      limits = [coords.min(), coords.max()]
  
      # Get elevation and azimut from view
      elev, azim = _get_view_plot_surf_matplotlib(hemi, view)
  
      # if no cmap is given, set to matplotlib default
      if cmap is None:
          cmap = plt.get_cmap(plt.rcParamsDefault["image.cmap"])
      # if cmap is given as string, translate to matplotlib cmap
      elif isinstance(cmap, str):
          cmap = plt.get_cmap(cmap)
  
      figsize = _default_figsize
      # Leave space for colorbar
      if colorbar:
          figsize[0] += 0.7
      # initiate figure and 3d axes
      if axes is None:
          if figure is None:
              figure = plt.figure(figsize=figsize)
          axes = figure.add_axes((0, 0, 1, 1), projection="3d")
      else:
          if figure is None:
              figure = axes.get_figure()
      axes.set_xlim(*limits)
      axes.set_ylim(*limits)
      axes.view_init(elev=elev, azim=azim)
      axes.set_axis_off()
  
      # plot mesh without data
      p3dcollec = axes.plot_trisurf(
          coords[:, 0],
          coords[:, 1],
          coords[:, 2],
          triangles=faces,
          linewidth=0.1,
          antialiased=False,
          color="white",
      )
  
      # reduce viewing distance to remove space around mesh
      axes.set_box_aspect(None, zoom=1.3)
  
      bg_face_colors = _compute_facecolors_matplotlib(
          bg_map, faces, coords.shape[0], darkness, alpha
      )
      if surf_map is not None:
          surf_map_faces = _compute_surf_map_faces_matplotlib(
              surf_map,
              faces,
              avg_method,
              coords.shape[0],
              bg_face_colors.shape[0],
          )
          (
              surf_map_faces,
              kept_indices,
              vmin,
              vmax,
          ) = _threshold_and_rescale(
              surf_map_faces, threshold, vmin, vmax
          )
  
          surf_map_face_colors = cmap(surf_map_faces)
          # set transparency of voxels under threshold to 0
          surf_map_face_colors[~kept_indices, 3] = 0
          if bg_on_data:
              # if need be, set transparency of voxels above threshold to 0.7
              # so that background map becomes visible
              surf_map_face_colors[kept_indices, 3] = 0.7
  
          face_colors = mix_colormaps(
              surf_map_face_colors, bg_face_colors
          )
  
          if colorbar:
              cbar_vmin = cbar_vmin if cbar_vmin is not None else vmin
              cbar_vmax = cbar_vmax if cbar_vmax is not None else vmax
              ticks = _get_ticks_matplotlib(
                  cbar_vmin, cbar_vmax, cbar_tick_format, threshold
              )
              our_cmap, norm = _get_cmap_matplotlib(
                  cmap, vmin, vmax, cbar_tick_format, threshold
              )
              bounds = np.linspace(cbar_vmin, cbar_vmax, our_cmap.N)
  
              # we need to create a proxy mappable
              proxy_mappable = ScalarMappable(cmap=our_cmap, norm=norm)
              proxy_mappable.set_array(surf_map_faces)
              cax, kw = make_axes(
                  axes,
                  location="right",
                  fraction=0.15,
                  shrink=0.5,
                  pad=0.0,
                  aspect=10.0,
              )
              figure.colorbar(
                  proxy_mappable,
                  cax=cax,
                  ticks=ticks,
                  boundaries=bounds,
                  spacing="proportional",
                  format=cbar_tick_format,
                  orientation="vertical",
              )
  
          p3dcollec.set_facecolors(face_colors)
          p3dcollec.set_edgecolors(face_colors)
  
      if title is not None:
          axes.set_title(title)
      # save figure if output file is given
      if output_file is not None:
          figure.savefig(output_file)
          plt.close()
      else:
          return figure
  
imports: import itertools
  import math
  from collections.abc import Sequence
  from warnings import warn
  import matplotlib
  import matplotlib.pyplot
  import numpy
  from matplotlib import gridspec
  from matplotlib.cm import ScalarMappable
  from matplotlib.colorbar import make_axes
  from matplotlib.colors import LinearSegmentedColormap, Normalize, to_rgba
  from matplotlib.patches import Patch
  from mpl_toolkits.mplot3d.art3d import Poly3DCollection
  from nilearn import image, surface
  from nilearn._utils import check_niimg_3d, compare_version, fill_doc
  from nilearn._utils.helpers import is_kaleido_installed, is_plotly_installed
  from nilearn.plotting.cm import cold_hot, mix_colormaps
  from nilearn.plotting.displays._slicers import _get_cbar_ticks
  from nilearn.plotting.html_surface import get_vertexcolor
  from nilearn.plotting.img_plotting import get_colorbar_and_data_ranges
  from nilearn.plotting.js_plotting_utils import colorscale
  from nilearn.surface import load_surf_data, load_surf_mesh, vol_to_surf
  from nilearn.surface.surface import check_mesh
label_desc: Use image node_id 17 for calling a global function with example usage: _plot_surf_matplotlib(coords, faces, surf_map, bg_map, hemi, view, cmap, colorbar, avg_method, threshold, alpha, bg_on_data, darkness, vmin, vmax, cbar_vmin, cbar_vmax, cbar_tick_format, title, title_font_size, output_file, axes, figure) and returns: figure