Skip to content

Fix: stop forcing matplotlib Agg backend on import (closes #2) - #4

Merged
Par-python merged 4 commits into
masterfrom
2-enforcing-matplotlibuseagg
Jun 3, 2026
Merged

Fix: stop forcing matplotlib Agg backend on import (closes #2)#4
Par-python merged 4 commits into
masterfrom
2-enforcing-matplotlibuseagg

Conversation

@Par-python

Copy link
Copy Markdown
Owner

Closes #2.

_core.py called matplotlib.use("Agg") at import time, and every measure
imports _core — so from entroscope import shannon silently switched the
user's whole matplotlib backend, breaking interactive plotting like
df["feature"].plot() in notebooks (reported by @StatisticianOne).

Fix

  • Remove the forced backend from _core.py; let matplotlib resolve its own
    (it already falls back to a non-interactive backend when there's no display).
  • The library no longer mutates global matplotlib state on import.

Headless safety moved to the environment

  • tests/conftest.py forces Agg for the test session.
  • CI test job sets MPLBACKEND: Agg.
  • README documents MPLBACKEND=Agg (and ENV MPLBACKEND=Agg for Docker) for
    anyone who wants a guaranteed non-interactive backend.

I went slightly further than the suggested "move it into utils.plot", since
that would still flip the backend globally on every plot call. Nothing in the
library touches the global backend now.

Regression guard

tests/test_backend.py imports entroscope in a subprocess under a non-Agg
backend and asserts it stays put (a subprocess is needed because conftest forces
Agg session-wide). This locks the fix in.

Verification

147 passed, coverage 98.74%, ruff check + format clean. Confirmed the reported
scenario: MPLBACKEND=template ... import shannon leaves the backend as
template, not agg.

@Par-python Par-python linked an issue Jun 3, 2026 that may be closed by this pull request
@Par-python
Par-python merged commit 982abb0 into master Jun 3, 2026
8 checks passed
@Par-python Par-python mentioned this pull request Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enforcing matplotlib.use("Agg")

1 participant