Skip to content

Fix: Compatibility with newer Matplotlib versions (handles MatplotlibDeprecationWarning) - #43

Open
andrebadini wants to merge 2 commits into
e-hulten:masterfrom
andrebadini:master
Open

Fix: Compatibility with newer Matplotlib versions (handles MatplotlibDeprecationWarning)#43
andrebadini wants to merge 2 commits into
e-hulten:masterfrom
andrebadini:master

Conversation

@andrebadini

Copy link
Copy Markdown

This pull request addresses an AttributeError that occurs when using july with recent versions of Matplotlib. The error is caused by the removal or renaming of MatplotlibDeprecationWarning in Matplotlib.

The fix implemented in this PR modifies the update_rcparams function in rcmod.py to handle the absence of MatplotlibDeprecationWarning gracefully. It now attempts to filter MatplotlibDeprecationWarning and, if that fails (due to the attribute not being present), it falls back to filtering DeprecationWarning. This approach ensures that the code works correctly with both older and newer Matplotlib releases.

This change should resolve issues where users encounter the following error:
AttributeError: module 'matplotlib.cbook' has no attribute 'MatplotlibDeprecationWarning'. Did you mean: 'VisibleDeprecationWarning'?

Fix: Handle MatplotlibDeprecationWarning for compatibility with newer Matplotlib versions

The `MatplotlibDeprecationWarning` has been removed or renamed in recent versions of Matplotlib, causing an `AttributeError` when `july` is used with those versions.  

This commit modifies the `update_rcparams` function in `rcmod.py` to use a more robust approach to filtering warnings. It attempts to filter `MatplotlibDeprecationWarning` for compatibility with older Matplotlib versions and falls back to filtering the more general `DeprecationWarning` if the former is not available. 

This ensures compatibility with a wider range of Matplotlib versions.
flokli added a commit to flokli/july that referenced this pull request Apr 28, 2025
This code was responsible from hiding deprecation warnings if a key gets
unused, but it causes more harm than good:

mpl did refactor their deprecation system, and the code simply failed as
the old mpl.cbook.MatplotlibDeprecationWarning didn't exist anymore.

Instead of going the approach of e-hulten#39 or e-hulten#43 again and adjusting to the
change, simply drop the deprecation warning hiding alltogether.

It'll show when july starts using deprecated keys, so they can be
updated.

Also use `mpl.rcParams[key] = value` directly and drop the ConfigDict
logic, as it's unneeded.

Closes e-hulten#39.
Closes e-hulten#43.
@flokli

flokli commented Apr 28, 2025

Copy link
Copy Markdown

Alternative approach: #44

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.

2 participants