Skip to main content

Mosaiq

The Mosaiq library introduces mosaiq as a Python function that generates a mosaic plot using Altair, supporting both categorical and numeric fields. This versatile visualization tool automatically bins numeric data and consolidates low-frequency categories into a single "TOP_NA" group. The plot is designed to provide a clear overview of the distribution and relationship between two fields, with customizable color schemes.

Features

  • Automatic Binning for Numeric Data: Uses histogram binning for numeric fields based on a specified maximum number of bins.
  • Top-N Categories for Categorical Data: Keeps only the most frequent categories (up to max_bins), combining all others into a "TOP_NA" bin.
  • Customizable Color Scheme: A single color parameter controls the color scheme for both fields, allowing a unified look across all bins.
  • Tooltip Support: Hovering over bins displays detailed information, including category labels and counts.

Dependencies

Installation

Install the required Python packages with:

pip install mosaiq 

Usage

Function Signature

mosaiq(dataframe: FrameT, field1: str, field2: str, max_bins=6, color="category20")

Parameters

  • dataframe (FrameT): A pandas DataFrame containing the data to be visualized.
  • field1 (str): Name of the first field (categorical or numeric) to display on the x-axis.
  • field2 (str): Name of the second field (categorical or numeric) to display as blocks within the mosaic.
  • max_bins (int, optional): Maximum number of bins or categories to display. Defaults to 6.
  • color (str, optional): Color scheme for all bins. Defaults to "category20".
  • top_na_label (str, optional): Provide a custom label for category bins that do not pass max_bins threshold.

Returns

  • altair.Chart: A compound Altair chart representing the mosaic plot.

Example Usage

from vega_datasets import data
mosaiq(data.seattle_weather(), "weather", "wind").configure_view(continuousWidth=900)

Seattle Weather

import pandas as pd
import narwhals as nw
from mosaiq import mosaiq

# Create a sample DataFrame
data = {
    "Category": ["A", "B", "C", "D", "E", "F", "G", "H"],
    "Value": [10, 15, 7, 30, 45, 10, 22, 5]
}
df = pd.DataFrame(data)

# Generate a mosaic plot
chart = mosaiq(df, "Category", "Value", max_bins=5, color="blueorange")
chart.display()

Color Argument Example

Customization

  • Adjust Binning: Control the number of bins for numeric fields with max_bins. If more categories than max_bins are present, the function groups the least frequent categories into a new "TOP_NA" category.
  • Color Scheme: Set a color scheme using any valid Altair color scheme name (e.g., "blues", "viridis", "category10"). This single color parameter unifies the plot’s appearance.

Additional Notes

This function is decorated with @nw.narwhalify to handle non-pandas DataFrame input using Narwhals typing. If you’re unfamiliar with Narwhals, check out the Narwhals GitHub repo for further information.

License

MIT License. See LICENSE for more information.


Enjoy exploring your data with Mosaiq!

Metadata

Release files for mosaiq 0.3.6

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

Built distribution (wheel)

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

Release files / mosaiq-0.3.6-py3-none-any.whl

Download URL mosaiq-0.3.6-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
178923d829330ad4f464136867b68d589892c4892d2a53b590d9269daf83fc41
BLAKE2b-256 checksum
How to use checksums
f5a60cc70113fddee6eb56dbae60f89eaffa9d49ae11d8d2604c67082f5c7f6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.3.6 This release

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.11

1 release file

0.1

1 release file

0.0.0

1 release file

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