From b51e6d110010d522e59bc800f3401f0f9ef51429 Mon Sep 17 00:00:00 2001 From: Boyu Dai Date: Sun, 4 Jan 2026 18:48:24 +1100 Subject: [PATCH 1/2] Log warning during ..plot to sphinx logger --- lib/matplotlib/sphinxext/plot_directive.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/sphinxext/plot_directive.py b/lib/matplotlib/sphinxext/plot_directive.py index dbf5072e2c93..b56f3879b166 100644 --- a/lib/matplotlib/sphinxext/plot_directive.py +++ b/lib/matplotlib/sphinxext/plot_directive.py @@ -196,6 +196,7 @@ import sys import textwrap import traceback +import warnings from docutils.parsers.rst import directives, Directive from docutils.parsers.rst.directives.images import Image @@ -584,9 +585,10 @@ def _run_code(code, code_path, ns=None, function_name=None): dirname = os.path.abspath(os.path.dirname(code_path)) os.chdir(dirname) - with cbook._setattr_cm( + with (warnings.catch_warnings() as caught_warnings, + cbook._setattr_cm( sys, argv=[code_path], path=[os.getcwd(), *sys.path]), \ - contextlib.redirect_stdout(StringIO()): + contextlib.redirect_stdout(StringIO())): try: if ns is None: ns = {} @@ -609,6 +611,12 @@ def _run_code(code, code_path, ns=None, function_name=None): raise PlotError(traceback.format_exc()) from err finally: os.chdir(pwd) + for warn in caught_warnings: + _log.warning( + "[plot] Python warning during plot execution: %s (%s)", + warn.message, + warn.category.__name__ + ) return ns From bf7a2debd8c45403acc4e7e8d2ab77323811c50b Mon Sep 17 00:00:00 2001 From: Boyu Dai Date: Sun, 4 Jan 2026 19:24:38 +1100 Subject: [PATCH 2/2] Correct python warning manager parameter --- lib/matplotlib/sphinxext/plot_directive.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/sphinxext/plot_directive.py b/lib/matplotlib/sphinxext/plot_directive.py index b56f3879b166..398469eec2e8 100644 --- a/lib/matplotlib/sphinxext/plot_directive.py +++ b/lib/matplotlib/sphinxext/plot_directive.py @@ -585,7 +585,7 @@ def _run_code(code, code_path, ns=None, function_name=None): dirname = os.path.abspath(os.path.dirname(code_path)) os.chdir(dirname) - with (warnings.catch_warnings() as caught_warnings, + with (warnings.catch_warnings(record=True) as caught_warnings, cbook._setattr_cm( sys, argv=[code_path], path=[os.getcwd(), *sys.path]), \ contextlib.redirect_stdout(StringIO())): @@ -611,6 +611,7 @@ def _run_code(code, code_path, ns=None, function_name=None): raise PlotError(traceback.format_exc()) from err finally: os.chdir(pwd) + for warn in caught_warnings: _log.warning( "[plot] Python warning during plot execution: %s (%s)",