Skip to main content

MosaicShade MosaicShade logo

PyPI version Python versions Tests Downloads License

Overview

Implements residual-based shading for mosaic displays in Python.

Shading based on residuals offers a way of visualizing how your data departs from a given statistical model. Currently, MosaicShade only uses the model of independence (see Limitations).

Colour and shading intensity respectively indicate the direction and magnitude of residuals, making patterns of association between categorical variables easier to identify.

MosaicShade extends statsmodels.graphics.mosaicplot.mosaic. The shading scheme used was inspired by shading_Friendly from the vcd package in the programming language R.

Installation

Install MosaicShade from PyPI:

python -m pip install mosaicshade

Usage

Currently, data needs to be a pd.DataFrame to be accepted by the mosaic function (see Limitations). The below is a made-up pd.DataFrame that will be used for subsequent examples:

import pandas as pd

df = pd.DataFrame({
    "Treatment": ["A", "A", "A", "A", "B", "B", "B", "B"],
    "Outcome": ["Good", "Good", "Bad", "Bad"] * 2,
    "Sex": ["Female", "Male"] * 4,
    "Freq": [20, 23, 3, 4, 2, 2, 10, 72]
})

df
Treatment Outcome Sex Freq
0 A Good Female 20
1 A Good Male 23
2 A Bad Female 3
3 A Bad Male 4
4 B Good Female 2
5 B Good Male 2
6 B Bad Female 10
7 B Bad Male 72

To make a mosaic from this data, we can call the mosaicshade.mosaic function, specifying the frequency column with the freq argument:

import mosaicshade
import matplotlib.pyplot as plt

mosaicshade.mosaic(df, freq='Freq')
plt.show() # Show the plot

Three-dimensional shaded mosaic plot

We can optionally select specific dimensions (columns) to use in this plot, summing over those that are excluded:

mosaicshade.mosaic(
    df,
    freq='Freq',
    dims=['Treatment', 'Outcome']
)
plt.show()

Two-dimensional shaded mosaic plot

Remaining keyword arguments from statsmodels.graphics.mosaicplot.mosaic also work (with the exception of the properties argument, as this is used to perform the shading). For example, here is how you may add a title to the above plot:

mosaicshade.mosaic(
    df,
    freq='Freq',
    dims=['Treatment', 'Outcome'],
    title='Shaded Mosaic'
)
plt.show()

Two-dimensional shaded mosaic plot with a title

API

mosaicshade.mosaic(df, freq, dims=None, **kwargs)

Used to construct mosaic displays. An extension of statsmodels.graphics.mosaicplot.mosaic that implements residual-based shading.

Parameters:

df : pd.Dataframe

The data to plot.

freq : str

The frequency column of your DataFrame.

dims : list[str] or tuple[str, …], optional

Two to four dimensions (column names) to use for the mosaic. Defaults to all dimensions except the frequency column.

**kwargs

Any remaining keyword arguments passed to statsmodels.graphics.mosaicplot.mosaic. Note that the properties argument is managed by mosaicshade and cannot be changed.

Returns:

fig : (matplotlib.figure.Figure)

The figure containing the mosaic plot.

ax : (matplotlib.axes.Axes)

The axes containing the mosaic plot.

Limitations

I am working to address these limitations in future versions. You are also welcome to contribute to help in this regard (see the Contributing section below).

  • Data input to the mosaicshade.mosaic() function must be of type pd.DataFrame
  • Residuals are currently calculated assuming a model of mutual independence
  • No way to adjust shading limits (e.g., changing from magnitude 2-4 for light and 4+ for dark)
  • Only labelling scheme added so far is labeling_values
  • Limited to 2-4 dimensions (i.e., 5+ dimensions are untested and currently blocked by the mosaic function)

Contributing

Contributions, bug reports, and feature requests are welcome. Please open an issue or submit a pull request. Contributions addressing the above limitations are especially appreciated.

Citation

If you use MosaicShade, please cite the software using information found in CITATION.cff or GitHub’s “Cite this repository” menu.

References

Friendly, M. (1994). Mosaic Displays for Multi-Way Contingency Tables. Journal of the American Statistical Association, 89(425), 190–200. https://doi.org/10.1080/01621459.1994.10476460

Meyer, D., Zeileis, A., Hornik, K., & Friendly, M. (2024). vcd: Visualizing Categorical Data. R package version 1.4-13. https://doi.org/10.32614/CRAN.package.vcd

Zeileis, A., Meyer, D., & Hornik, K. (2007). Residual-based Shadings for Visualizing (Conditional) Independence. Journal of Computational and Graphical Statistics, 16(3), 507-525. https://doi.org/10.1198/106186007X237856

License

MosaicShade is licensed under the GNU General Public License version 3 (GPL-3.0-only). See LICENSE for details.

Metadata

Release files for mosaicshade 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mosaicshade 0.1.1
File Size Uploaded Provenance
mosaicshade-0.1.1.tar.gz 21.4 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mosaicshade 0.1.1
File Interpreter ABI Platform
mosaicshade-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 42.0 kB

Release files / mosaicshade-0.1.1.tar.gz

Download URL mosaicshade-0.1.1.tar.gz
Size 21.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4f2ed37a4547ef61509641ce6c2870ec11d6ef7005250abbe59d039fbb9a7e3d
BLAKE2b-256 checksum
How to use checksums
d613155a7d63859c98a2ec5b9ab05aed3f33cc386e75568f3f25430eca37bb76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 15, 2026.

Transparency log

Release files / mosaicshade-0.1.1-py3-none-any.whl

Download URL mosaicshade-0.1.1-py3-none-any.whl
Size 20.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
39c17d1ec4914cbfd62d2d45dbe25631bc2098aaf34f81e83c34145f78718d20
BLAKE2b-256 checksum
How to use checksums
6fdc9e526186758f63cec01fe911f26bfd634352b13655624c515d893f2cfc70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 15, 2026.

Transparency log

Security for mosaicshade 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mosaicshade-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page