code: def _annotate_heatmap(
      im: matplotlib.image.AxesImage,
      data: Optional[np.ndarray] = None,
      fmt: Union[str, matplotlib.ticker.Formatter] = "{x:.2f}",
      textcolors: Tuple[str, str] = ("black", "white"),
      threshold: Optional[float] = None,
      **kwargs,
  ):
      """
      A function to annotate a heatmap. The code is adapted from `matplotlib tutorials`_.
  
      .. _matplotlib tutorials:
          https://matplotlib.org/stable/gallery/images_contours_and_fields/image_annotated_heatmap.html
  
      Parameters
      ----------
      im
          The `matplotlib.image.AxesImage` to be labeled.
      data
          Optional 2D `numpy` array of shape `M x N` used to annotate the cells.  If ``None``, the image's data is used.
      fmt
          Format of the annotations inside the heatmap. This should either use the string format method,
          e.g. ``"{x:.2f}"``, or be a `matplotlib.ticker.Formatter`. Default ``"{x:.2f}"``.
      textcolors
          A tuple of `matplotlib` colors. The first is used for values below a threshold, the second for those above.
          Default ``('black', 'white')``.
      threshold
          Optional value in data units according to which the colors from `textcolors` are applied.
          If ``None`` (the default) uses the middle of the colormap as separation.
      **kwargs
          All other keyword arguments are passed to `matplotlib.axes.Axes.text`_.
  
          .. _matplotlib.axes.Axes.text:
              https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.text.html
      """
  
      if not isinstance(data, (list, np.ndarray)):
          data = im.get_array()
          assert isinstance(
              data, np.ndarray
          )  # for mypy, since get_array() can sometimes be None
  
      # normalize the threshold to the images color range.
      if threshold is not None:
          threshold = im.norm(threshold)
      else:
          threshold = im.norm(np.max(data)) / 2.0
  
      # set default alignment to center, but allow it to be overwritten by textkw.
      kw = dict(
          horizontalalignment="center", verticalalignment="center"
      )
      kw.update(kwargs)
  
      # get the formatter in case a string is supplied
      if isinstance(fmt, str):
          fmt = matplotlib.ticker.StrMethodFormatter(fmt)
  
      # loop over the data and create a `Text` for each "pixel".
      # Change the text's color depending on the data.
      texts = []
      for i in range(data.shape[0]):
          for j in range(data.shape[1]):
              kw.update(
                  color=textcolors[int(im.norm(data[i, j]) > threshold)]
              )
              text = im.axes.text(j, i, fmt(data[i, j], None), **kw)  # type: ignore[arg-type]
              texts.append(text)
  
      return texts
  
imports: import warnings
  from enum import Enum
  from typing import List, Optional, Tuple, Union
  import matplotlib
  import numpy
  from matplotlib import pyplot
  from matplotlib.colors import LinearSegmentedColormap
  from matplotlib.pyplot import Axes, Figure
  from mpl_toolkits.axes_grid1 import make_axes_locatable
  from numpy import ndarray
label_desc: Use image node_id 7 for calling a global function with example usage: _annotate_heatmap(im, data, fmt, textcolors, threshold) and returns: texts