Skip to content

mosaic_heatmap

mosaic_heatmap(data, ...)

Plot mosaic data as a color-encoded matrix.

Creates a mosaic heatmap where the column widths and row heights are proportional to the marginal sums of the data matrix.

Parameters:

Name Type Description Default
data array - like

2D dataset that can be coerced into an ndarray. If a pandas DataFrame is provided, the index/column information will be used to label the columns and rows.

required
vmin float

Values to anchor the colormap.

None
vmax float

Values to anchor the colormap.

None
cmap str or Colormap

The mapping from data values to color space.

None
center float

The value at which to center the colormap for divergent data.

None
robust bool

If True, compute colormap range using robust quantiles.

False
annot bool or array - like

If True, write the data value in each cell.

None
fmt str

String formatting code for annotation values. Default: '.2g'

'.2g'
annot_kws dict

Keyword arguments for annotation text.

None
cbar bool

Whether to draw a colorbar. Default: True

True
cbar_kws dict

Keyword arguments for colorbar.

None
cbar_ax Axes

Axes in which to draw the colorbar.

None
square bool

If True, set aspect ratio to "equal". Default: False

False
xticklabels (auto, bool, array - like)

Tick label specification.

'auto'
yticklabels (auto, bool, array - like)

Tick label specification.

'auto'
mask bool array or DataFrame

If True in a cell, data is not shown.

None
ax Axes

Axes in which to draw the plot. Uses current axes if None.

None
**kwargs dict

Additional keyword arguments passed to pcolormesh.

{}

Returns:

Type Description
Axes

The Axes object with the heatmap.

Examples:

>>> import numpy as np
>>> from mheatmap import mosaic_heatmap
>>> data = np.array([[10, 2, 0], [1, 8, 3], [0, 1, 12]])
>>> ax = mosaic_heatmap(data, annot=True, cmap='YlOrRd')
Source code in src/mheatmap/matrix.py
def mosaic_heatmap(
    data,
    *,
    vmin=None,
    vmax=None,
    cmap=None,
    center=None,
    robust=False,
    annot=None,
    fmt=".2g",
    annot_kws=None,
    cbar=True,
    cbar_kws=None,
    cbar_ax=None,
    square=False,
    xticklabels="auto",
    yticklabels="auto",
    mask=None,
    ax=None,
    **kwargs,
):
    """`mosaic_heatmap(data, ...)`

    Plot mosaic data as a color-encoded matrix.

    Creates a mosaic heatmap where the column widths and row
    heights are proportional to the marginal sums of the data
    matrix.

    Parameters
    ----------
    data : array-like
        2D dataset that can be coerced into an ndarray. If a
        pandas DataFrame is provided, the index/column information
        will be used to label the columns and rows.
    vmin, vmax : float, optional
        Values to anchor the colormap.
    cmap : str or matplotlib.colors.Colormap, optional
        The mapping from data values to color space.
    center : float, optional
        The value at which to center the colormap for divergent data.
    robust : bool, optional
        If True, compute colormap range using robust quantiles.
    annot : bool or array-like, optional
        If True, write the data value in each cell.
    fmt : str, optional
        String formatting code for annotation values. Default: '.2g'
    annot_kws : dict, optional
        Keyword arguments for annotation text.
    cbar : bool, optional
        Whether to draw a colorbar. Default: True
    cbar_kws : dict, optional
        Keyword arguments for colorbar.
    cbar_ax : matplotlib.axes.Axes, optional
        Axes in which to draw the colorbar.
    square : bool, optional
        If True, set aspect ratio to "equal". Default: False
    xticklabels, yticklabels : 'auto', bool, array-like, optional
        Tick label specification.
    mask : bool array or DataFrame, optional
        If True in a cell, data is not shown.
    ax : matplotlib.axes.Axes, optional
        Axes in which to draw the plot. Uses current axes if None.
    **kwargs : dict
        Additional keyword arguments passed to pcolormesh.

    Returns
    -------
    matplotlib.axes.Axes
        The Axes object with the heatmap.

    Examples
    --------
    >>> import numpy as np
    >>> from mheatmap import mosaic_heatmap
    >>> data = np.array([[10, 2, 0], [1, 8, 3], [0, 1, 12]])
    >>> ax = mosaic_heatmap(data, annot=True, cmap='YlOrRd')
    """
    plotter = _MosaicHeatMapper(
        data,
        vmin,
        vmax,
        cmap,
        center,
        robust,
        annot,
        fmt,
        annot_kws,
        cbar,
        cbar_kws,
        xticklabels,
        yticklabels,
        mask,
    )

    if ax is None:
        ax = plt.gca()
    if square:
        ax.set_aspect("equal")
    plotter.plot(ax, cbar_ax, kwargs)
    return ax