Skip to main content

No project description provided

Project description

collint

Detect and auto-fix visual collisions in matplotlib figures.

collint inspects a rendered figure and finds overlapping or clipped elements:

  • text-text: tick labels, annotations, axis labels, titles, or legend text overlapping each other
  • clipping: titles, axis labels, or tick labels cut off by the figure edge
  • over-data: a legend or annotation covering plotted data
  • element-element: overlapping subplots, colorbars, or a suptitle colliding with subplot titles

Detection is geometry-based over the rendered bounding boxes. The over-data case is confirmed by sampling the pixels underneath the decoration, so a legend placed over an empty region of a sparse plot is not falsely reported.

Install

Building requires a Rust toolchain (the core is a compiled extension).

pip install git+https://github.com/alejandro-soto-franco/collint

Or from a clone:

git clone https://github.com/alejandro-soto-franco/collint
cd collint
pip install .

Supported Python versions: 3.10 to 3.13.

Quick start

import matplotlib.pyplot as plt
import collint

fig, ax = plt.subplots(figsize=(2, 2))
ax.plot(range(100), range(100))
ax.set_xticks(range(0, 100))
ax.set_xticklabels([f"label{i}" for i in range(100)])

report = collint.check(fig)
print(report.has_collisions)        # True
print(report.severity)              # worst overlap fraction
print(report.to_json())             # machine-readable list

check never mutates the figure. It draws the figure on an Agg renderer if needed, so it works headless.

The report

check returns a CollisionReport:

report = collint.check(fig)

report.has_collisions      # bool
report.collisions          # list of Collision
report.severity            # max severity across collisions (0.0 if none)
report.total()             # summed severity
report.exit_code()         # 0 if clean, 1 if any collision
report.by_type("text_text")  # collisions of one kind
report.to_json()           # JSON string

Each Collision carries the two elements involved:

for c in report.collisions:
    print(c.kind)           # "text_text" | "clipping" | "over_data" | "element_element"
    print(c.a_role, c.a_id) # e.g. "tick-label", "axes0.tick.140..."
    print(c.a_box)          # (x0, y0, x1, y1) in display pixels
    print(c.b_role, c.b_id)
    print(c.b_box)
    print(c.severity)       # overlap fraction, or clip overhang in pixels

Auto-fixing

autofix tries to resolve collisions by applying matplotlib's own layout tools (constrained layout, tight layout, legend relocation, tick rotation), re-checking after each step and keeping a change only if it does not make things worse.

result = collint.autofix(fig)
print(result.actions)            # e.g. ["constrained_layout"]
print(result.residual.has_collisions)  # what remains after fixing

autofix reports exactly which steps it applied. It never returns a figure worse than the one it received.

Annotated overlay

Save a copy of the figure with every colliding region outlined in red:

from collint.overlay import render_overlay

report = collint.check(fig)
render_overlay(fig, report, "overlay.png")

The overlay is written to disk and the original figure is left unchanged.

Command line

Point the CLI at a Python file that defines a module-level fig. It prints the JSON report and exits non-zero when collisions are found, which makes it easy to gate a figure-generation pipeline.

collint myfigure.py
collint myfigure.py --overlay overlay.png
collint myfigure.py --no-pixels      # skip pixel confirmation, geometry only
# myfigure.py
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 0])

Exit code is 0 when the figure is clean and 1 when it has collisions.

pytest integration

Installing collint registers an assert_no_collisions fixture:

def test_my_plot_is_clean(assert_no_collisions):
    fig = build_my_plot()
    assert_no_collisions(fig)

The assertion fails with the JSON report when the figure has collisions.

savefig hook

Opt in to a warning every time a figure with collisions is saved:

import collint
collint.install()

fig.savefig("out.png")   # warns if out.png has visual collisions

Configuration

Thresholds can be passed to check (and through autofix):

collint.check(
    fig,
    use_pixels=True,        # confirm over-data collisions by sampling pixels
    overlap_fraction=0.02,  # minimum overlap (fraction of the smaller box)
    clip_px=1.0,            # minimum clip overhang in pixels
    ink_fraction=0.05,      # minimum data ink under a decoration to confirm over-data
)

Defaults ignore sub-pixel and antialiasing noise. Set use_pixels=False to run geometry-only detection, which is faster but reports any legend or annotation whose bounding box overlaps a data artist, even over empty regions.

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

collint-0.1.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distributions

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

collint-0.1.0-cp310-abi3-win_amd64.whl (233.7 kB view details)

Uploaded CPython 3.10+Windows x86-64

collint-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (406.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

collint-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (399.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

collint-0.1.0-cp310-abi3-macosx_11_0_arm64.whl (348.6 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

collint-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl (350.9 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for collint-0.1.0.tar.gz
Algorithm Hash digest
SHA256 de988bcaf35088c8b28886e8a7a58147ea61993b5b510cafaca2cbfbe375e58f
MD5 7753863270d85ad33007396efb353e6f
BLAKE2b-256 981d5ef32d770ffd8626e84823883cfdfabd34e394608dd1ff8436af8bcc0883

See more details on using hashes here.

Provenance

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

Publisher: release.yml on alejandro-soto-franco/collint

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

File details

Details for the file collint-0.1.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: collint-0.1.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 233.7 kB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for collint-0.1.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6edd09bd18b386c88c1d25b3249b20a46aaaf6ac7ffd318d39aef4ed0b7abd6b
MD5 29630ac33c414bf46b6008769824f327
BLAKE2b-256 5a74944f3c71a084f86773343fbe73d5a66f4fccb42b77133caeda995c97e376

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.0-cp310-abi3-win_amd64.whl:

Publisher: release.yml on alejandro-soto-franco/collint

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

File details

Details for the file collint-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for collint-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ade14655312a294e00cf59238d41dd920e18ee86937fa7f13d71b0a75d8f8cc4
MD5 bc6e82d003fca20ee648756cdde563a8
BLAKE2b-256 01b6e27c5164e2a8c1084069ae0ba357404358cba4b5ceb60fd49a6a2edbfddc

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on alejandro-soto-franco/collint

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

File details

Details for the file collint-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for collint-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 669ad0f6f73d4831aa034321c65a21a68867b8414e1eb0750c95c6428465417c
MD5 a5083db29f8c4a39510d9e38194a72ff
BLAKE2b-256 6561e4998701ae62fccfbf1f88f6d7d15151f8ca45545c20a4bc4f66ae8a0f8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on alejandro-soto-franco/collint

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

File details

Details for the file collint-0.1.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for collint-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 19e6f33ea43e90a555f2159e27f380a3fc3ec05d09e165c392630f86e10ff2e5
MD5 432fa837975c8183103bf0a2d06dfcad
BLAKE2b-256 4f9c7da36917a8082de869ce5bf43a28f87cb465fffe183ea72c352805e81881

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.0-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on alejandro-soto-franco/collint

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

File details

Details for the file collint-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for collint-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d44fa828687144b95d7295473e646cd3587e7c539599e28f42ba55341d79c244
MD5 ee266c40f6505351ce7b4e9a5fd55282
BLAKE2b-256 5dbc4aaf7df53a945a7f5ce07e94441612bb6d82fb00cb19d37dbcddc7d0f80f

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on alejandro-soto-franco/collint

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