Skip to main content

Residual-based shading for mosaic plots in Python

Project description

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
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mosaicshade-0.1.0.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mosaicshade-0.1.0-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file mosaicshade-0.1.0.tar.gz.

File metadata

  • Download URL: mosaicshade-0.1.0.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mosaicshade-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e13dde19cd15a649603fab7d87673b9de8ca8f9827f0c02082b84d7ca1b19ea9
MD5 abb99d4efceea57947ebe98a28937765
BLAKE2b-256 63d4390a489192654f98685300114831168d2331d288f936093736786f793baa

See more details on using hashes here.

Provenance

The following attestation bundles were made for mosaicshade-0.1.0.tar.gz:

Publisher: publish.yml on gklorfine/mosaicshade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mosaicshade-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mosaicshade-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mosaicshade-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98d9c9d3828428668c51f254878eaa22a54252ad95ab77e68d64729af9983e88
MD5 f55f6f7a2f6975e4715ebb75b9e2b17b
BLAKE2b-256 851717234c8589de35b156dd9beee6045a43916276877b5742541e192a35c24b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mosaicshade-0.1.0-py3-none-any.whl:

Publisher: publish.yml on gklorfine/mosaicshade

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page