code: def plot_importances(
      df_importances,
      yrot=0,
      label_fontsize=10,
      width=4,
      minheight=1.5,
      vscale=1,
      imp_range=(-0.002, 0.15),
      color="#D9E6F5",
      bgcolor=None,  # seaborn uses '#F1F8FE'
      xtick_precision=2,
      title=None,
      ax=None,
  ):
      """
      Given an array or data frame of importances, plot a horizontal bar chart
      showing the importance values.
  
      :param df_importances: A data frame with Feature, Importance columns
      :type df_importances: pd.DataFrame
      :param width: Figure width in default units (inches I think). Height determined
                    by number of features.
      :type width: int
      :param minheight: Minimum plot height in default matplotlib units (inches?)
      :type minheight: float
      :param vscale: Scale vertical plot (default .25) to make it taller
      :type vscale: float
      :param label_fontsize: Font size for feature names and importance values
      :type label_fontsize: int
      :param yrot: Degrees to rotate feature (Y axis) labels
      :type yrot: int
      :param label_fontsize:  The font size for the column names and x ticks
      :type label_fontsize:  int
      :param scalefig: Scale width and height of image (widthscale,heightscale)
      :type scalefig: 2-tuple of floats
      :param xtick_precision: How many digits after decimal for importance values.
      :type xtick_precision: int
      :param xtick_precision: Title of plot; set to None to avoid.
      :type xtick_precision: string
      :param ax: Matplotlib "axis" to plot into
      :return: None
  
      SAMPLE CODE
  
      rf = RandomForestRegressor(n_estimators=100, n_jobs=-1, oob_score=True)
      X_train, y_train = ..., ...
      rf.fit(X_train, y_train)
      imp = importances(rf, X_test, y_test)
      viz = plot_importances(imp)
      viz.save('file.svg')
      viz.save('file.pdf')
      viz.view() # or just viz in notebook
      """
      I = df_importances
      unit = 1
      ypadding = 0.1
  
      imp = I.Importance.values
      mindrop = np.min(imp)
      maxdrop = np.max(imp)
      imp_padding = 0.002
      imp_range = (
          min(imp_range[0], mindrop - imp_padding),
          max(imp_range[1], maxdrop + imp_padding),
      )
  
      barcounts = np.array([f.count("\n") + 1 for f in I.index])
      N = np.sum(barcounts)
      ymax = N * unit + len(I.index) * ypadding + ypadding
      # print(f"barcounts {barcounts}, N={N}, ymax={ymax}")
      height = max(minheight, ymax * 0.2 * vscale)
  
      if ax is None:
          plt.close()
          fig, ax = plt.subplots(1, 1, figsize=(width, height))
      ax.set_xlim(*imp_range)
      ax.set_ylim(0, ymax)
      ax.spines["top"].set_linewidth(0.3)
      ax.spines["right"].set_linewidth(0.3)
      ax.spines["left"].set_linewidth(0.3)
      ax.spines["bottom"].set_linewidth(0.3)
      if bgcolor:
          ax.set_facecolor(bgcolor)
  
      yloc = []
      y = barcounts[0] * unit / 2 + ypadding
      yloc.append(y)
      for i in range(1, len(barcounts)):
          wprev = barcounts[i - 1]
          w = barcounts[i]
          y += (wprev + w) / 2 * unit + ypadding
          yloc.append(y)
      yloc = np.array(yloc)
      ax.xaxis.set_major_formatter(
          FormatStrFormatter(f"%.{xtick_precision}f")
      )
      # too close to show both max and right edge?
      if maxdrop / imp_range[1] > 0.9 or maxdrop < 0.02:
          ax.set_xticks([0, imp_range[1]])
      else:
          ax.set_xticks([0, maxdrop, imp_range[1]])
      ax.tick_params(labelsize=label_fontsize, labelcolor=GREY)
      ax.invert_yaxis()  # labels read top-to-bottom
      if title:
          ax.set_title(
              title,
              fontsize=label_fontsize + 1,
              fontname="Arial",
              color=GREY,
          )
  
      barcontainer = ax.barh(
          y=yloc,
          width=imp,
          height=barcounts * unit,
          tick_label=I.index,
          color=color,
          align="center",
      )
  
      # Alter appearance of each bar
      for rect in barcontainer.patches:
          rect.set_linewidth(0.5)
          rect.set_edgecolor(GREY)
  
      # rotate y-ticks
      if yrot is not None:
          ax.tick_params(labelrotation=yrot)
  
      return PimpViz()
  
imports: import pandas
  import numpy
  import matplotlib.pyplot
  import sklearn
  from sklearn.ensemble import RandomForestClassifier
  from sklearn.ensemble import RandomForestRegressor
  from distutils.version import LooseVersion
  from sklearn.model_selection import cross_val_score
  from sklearn.base import clone
  from sklearn.metrics import r2_score
  from sklearn.metrics import f1_score
  from sklearn.preprocessing import LabelEncoder
  from scipy import stats
  from pandas.api.types import is_numeric_dtype
  from matplotlib.colors import ListedColormap
  from matplotlib.ticker import FormatStrFormatter
  from copy import copy
  import warnings
  import tempfile
  from os import getpid, makedirs
label_desc: Use image node_id 16 for calling a global function with example usage: plot_importances(df_importances, yrot, label_fontsize, width, minheight, vscale, imp_range, color, bgcolor, xtick_precision, title, ax) and returns: PimpViz