MatplotlibDeprecationWarning default visibility in long-running scripts
0 reputation · 12 Dec 2020, 23:47 UTC
0 reputation · 12 Dec 2020, 23:47 UTC
Keep Matplotlib deprecation warnings visible long enough to act on them, without turning long-running scripts or notebooks into continuous notification noise. The scope is Matplotlib's own warnings, not third-party libraries.
Warning filters are global to Python's warnings module, so a broad filter can suppress important alerts from other packages. Matplotlib emits MatplotlibDeprecationWarning by default and does not filter it internally. The figure.max_open_warning rcParam adds a separate UserWarning threshold for open figures. Behavior and rcParam names can change between releases, so assume Matplotlib 3.x and recheck the target version.
It is unresolved whether MatplotlibDeprecationWarning should be opt-in or opt-out by default. Current behavior shows every deprecation warning, which supports migration but creates noise for users who cannot update immediately.
Can Matplotlib surface each deprecation warning once per process without global filter side effects? Should MatplotlibDeprecationWarning be opt-out by default in the next minor release? How should figure.max_open_warning interact with user-defined warning filters in interactive sessions?
35775 reputation · 13 Dec 2020, 10:51 UTC
MatplotlibDeprecationWarning is not filtered by default in any Matplotlib 3.x release. Python's warnings module applies once-per-file filtering to DeprecationWarning subclasses, so each unique deprecation appears only once per source file unless the warning state is reset.
Use warnings.filterwarnings() with explicit category targeting to limit noise while preserving visibility:
import warnings
from matplotlib.cbook import MatplotlibDeprecationWarning
warnings.filterwarnings('once', category=MatplotlibDeprecationWarning)
This ensures each distinct deprecation warning emits once per location, reducing noise in long-running processes without affecting third-party warnings.
warnings.filterwarnings('once', category=MatplotlibDeprecationWarning) before importing matplotlib.warnings._global_deprecation_count keys after triggering (internal API, subject to change).__file__ attribute variations.matplotlib.__version__ to confirm compatibility with the described warning tracking mechanism.The figure.max_open_warning rcParam triggers a separate UserWarning when open figures exceed the threshold (default 100). This operates independently of MatplotlibDeprecationWarning filtering and requires its own handling via rcParams or additional warning filters.
Whether MatplotlibDeprecationWarning should be opt-out by default remains unresolved. Current behavior favors migration visibility over noise reduction, so a targeted filter is the practical middle ground today.
Matplotlib's internal warning tracking may evolve between minor releases. Always verify behavior after upgrades by checking matplotlib.__version__ and testing known deprecation paths. The warnings._global_deprecation_count dictionary is a private API and could be removed without notice.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 13 Dec 2020, 02:14 UTC
One practical constraint is that Python's warnings registry is per module, so warnings.filterwarnings('once', category=MatplotlibDeprecationWarning) suppresses repeats per __warningregistry__, not strictly once per process. In a long-running notebook or script that re-imports or reloads modules, the same deprecation can reappear if the registry is cleared or if the warning originates in a different module. For Matplotlib 3.x, filtering on the narrow category avoids the collateral damage of ignoring DeprecationWarning globally, but the filter still applies process-wide.
Separately, figure.max_open_warning emits a UserWarning and is unaffected by a MatplotlibDeprecationWarning filter. To verify current behavior in your target version, run a short script with python -W always and inspect matplotlib.__init__ and matplotlib._api.deprecation for any built-in filterwarnings calls before relying on defaults.