code: def colorbar_factory(cax, mappable, **kwargs):
      """
      Creates a colorbar on the given axes for the given mappable.
  
      Typically, for automatic colorbar placement given only a mappable use
      :meth:`~matplotlib.figure.Figure.colorbar`.
  
      """
      # if the given mappable is a contourset with any hatching, use
      # ColorbarPatch else use Colorbar
      if isinstance(mappable, contour.ContourSet) and any(
          [hatch is not None for hatch in mappable.hatches]
      ):
          cb = ColorbarPatch(cax, mappable, **kwargs)
      else:
          cb = Colorbar(cax, mappable, **kwargs)
  
      cid = mappable.callbacksSM.connect(
          "changed", cb.on_mappable_changed
      )
      mappable.colorbar = cb
      mappable.colorbar_cid = cid
  
      return cb
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import six
  from six.moves import xrange, zip
  import warnings
  import numpy
  import matplotlib
  import matplotlib.artist
  import matplotlib.cbook
  import matplotlib.collections
  import matplotlib.colors
  import matplotlib.contour
  import matplotlib.cm
  import matplotlib.gridspec
  import matplotlib.patches
  import matplotlib.path
  import matplotlib.ticker
  import matplotlib.transforms
  import matplotlib._layoutbox
  import matplotlib._constrained_layout
  from matplotlib import docstring
label_desc: Use image node_id 4 for calling a global function with example usage: colorbar_factory(cax, mappable) and returns: cb