code: def mosaic_plot(
      data, rows, cols, vary_lightness=False, ax=None, legend=True
  ):
      """Create a mosaic plot from a dataframe.
  
      Right now only horizontal mosaic plots are supported,
      i.e. rows are prioritized over columns.
  
      Parameters
      ----------
      data : pandas data frame
          Data to tabulate.
      rows : column specifier
          Column in data to tabulate across rows.
      cols : column specifier
          Column in data to use to subpartition rows.
      vary_lightness : bool, default=False
          Whether to vary lightness across categories.
      ax : matplotlib axes or None
          Axes to plot into.
      legend : boolean, default=True
          Whether to create a legend.
  
      Examples
      --------
      >>> from dabl.datasets import load_titanic
      >>> data = load_titanic()
      >>> mosaic_plot(data, 'sex', 'survived')
      """
  
      cont = pd.crosstab(data[cols], data[rows])
      sort = np.argsort((cont / cont.sum()).iloc[0])
      cont = cont.iloc[:, sort]
      if ax is None:
          ax = plt.gca()
      pos_y = 0
      positions_y = []
      n_cols = cont.shape[1]
      colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
      for i, col in enumerate(cont.columns):
          height = cont[col].sum()
          positions_y.append(pos_y + height / 2)
  
          pos_x = 0
          for j, row in enumerate(cont[col]):
              width = row / height
              color = colors[j]
              if vary_lightness:
                  color = _lighten_color(color, (i + 1) / (n_cols + 1))
              rect = Rectangle(
                  (pos_x, pos_y),
                  width,
                  height,
                  edgecolor="k",
                  facecolor=color,
              )
              pos_x += width
              ax.add_patch(rect)
          pos_y += height
  
      if legend:
          legend_elements = [
              Patch(facecolor=colors[i], edgecolor="k")
              for i in range(len(cont.index))
          ]
          legend_labels = [str(index) for index in cont.index]
          ax.legend(legend_elements, legend_labels)
  
      ax.set_ylim(0, pos_y)
      ax.set_yticks(positions_y)
      ax.set_yticklabels(cont.columns)
  
imports: from warnings import warn
  from functools import reduce
  import itertools
  import numpy
  import pandas
  import matplotlib.pyplot
  from matplotlib.axes import Axes
  from matplotlib.patches import Rectangle, Patch
  from matplotlib.ticker import EngFormatter
  from seaborn.utils import despine
  from sklearn.tree import DecisionTreeClassifier
  from sklearn.metrics import roc_curve
  from sklearn.model_selection import cross_val_score, StratifiedShuffleSplit
  from ..preprocessing import detect_types
  from .._config import get_config
label_desc: Use image node_id 5 for calling a global function with example usage: mosaic_plot(data, rows, cols, vary_lightness, ax, legend) without return types