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.1.tar.gz (19.0 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.1-cp310-abi3-win_amd64.whl (239.2 kB view details)

Uploaded CPython 3.10+Windows x86-64

collint-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (415.7 kB view details)

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

collint-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (409.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

collint-0.1.1-cp310-abi3-macosx_11_0_arm64.whl (360.5 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

collint-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl (365.8 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: collint-0.1.1.tar.gz
  • Upload date:
  • Size: 19.0 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.1.tar.gz
Algorithm Hash digest
SHA256 a9ca314d5e9623d91a0930685ea0e7c738baa472ed5c12b9afb4ec803c8b6e31
MD5 720f1babbf75130c37fe04060ea86da3
BLAKE2b-256 aa2753d1568058676de20f26d4631a901d91fff4f1ee79abee3a10e96c6c4c42

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.1.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.1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: collint-0.1.1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 239.2 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.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9e1eb99d79241769532a9a1a0662978954c2ed7348755a68e3cc120fb5a73048
MD5 62521c00e39bac464c1f739604dc8bdf
BLAKE2b-256 50d1cde43dd33b0491e007df5d8c06f5066efb63bdd0d9de1db9fd6d779f9732

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.1-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.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for collint-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dc040f84d3771f3b185594ba9366939a04d30f202568b3d5395304841b433566
MD5 79db737df59c425cb107f316cf463d46
BLAKE2b-256 ab43ece2632efb054d4fdf8ae0284697a72d636084b2002a097c528256619b0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.1-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.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for collint-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 778c7d2b56ead702ef5f88323a0f34566886384395ab1e65e71a6fe120774e29
MD5 209c59c0b9ecf5ad34d2c858f1bda5e5
BLAKE2b-256 8ea202c32bf5bc47da3c34ccf6a8270019159ea9ca2e15e97c46c23cb1a1c5e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.1-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.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for collint-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5d0950b4737e2a951e091388bf76013dedb851dac468f55847bb5ae72803890e
MD5 a56788cca6f6b7be196e21e99bd0be18
BLAKE2b-256 a8cba26dd56a7c637b0b2afa843bc609f69a1371c91adfeab43152b41e8769bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.1-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.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for collint-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 629d3c5bd027798a67428e020107fd84e41e95d238616faf4ed5729d721fdc41
MD5 3e8374357993138bfb8c1a65db0f795d
BLAKE2b-256 cb34457fc7f35376dfce9532da4aefe5592f8d8970d3efdf1d2a21da17011bd2

See more details on using hashes here.

Provenance

The following attestation bundles were made for collint-0.1.1-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