Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e16b88d

Browse filesBrowse files
committed
add :filter-warning: option to plot directive
1 parent 8531423 commit e16b88d
Copy full SHA for e16b88d

File tree

3 files changed

+31
-11
lines changed
Filter options

3 files changed

+31
-11
lines changed

‎doc/users/prev_whats_new/whats_new_3.6.0.rst

Copy file name to clipboardExpand all lines: doc/users/prev_whats_new/whats_new_3.6.0.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ them in order to locate a required glyph.
553553
:caption: Demonstration of mixed English and Chinese text with font fallback.
554554
:alt: The phrase "There are 几个汉字 in between!" rendered in various fonts.
555555
:include-source: True
556+
:filter-warning: Glyph
556557

557558
plt.rcParams["font.size"] = 20
558559
fig = plt.figure(figsize=(4.75, 1.85))

‎galleries/users_explain/text/fonts.py

Copy file name to clipboardExpand all lines: galleries/users_explain/text/fonts.py
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@
180180
181181
.. plot::
182182
:include-source:
183+
:filter-warning: Glyph
183184
:caption: The string "There are 几个汉字 in between!" rendered with 2 fonts.
184185
186+
185187
fig, ax = plt.subplots()
186188
ax.text(
187189
.5, .5, "There are 几个汉字 in between!",

‎lib/matplotlib/sphinxext/plot_directive.py

Copy file name to clipboardExpand all lines: lib/matplotlib/sphinxext/plot_directive.py
+28-11Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@
7878
figure. This overwrites the caption given in the content, when the plot
7979
is generated from a file.
8080
81+
``:filter-warning:`` : str
82+
When specified, will ignore warnings that match the input string, which is
83+
a warnings filter `message regex <msg>`_ string.
84+
85+
.. _msg: https://docs.python.org/3/library/warnings.html#the-warnings-filter
86+
8187
Additionally, this directive supports all the options of the `image directive
8288
<https://docutils.sourceforge.io/docs/ref/rst/directives.html#image>`_,
8389
except for ``:target:`` (since plot will add its own target). These include
@@ -177,6 +183,7 @@
177183
import sys
178184
import textwrap
179185
import traceback
186+
import warnings
180187

181188
from docutils.parsers.rst import directives, Directive
182189
from docutils.parsers.rst.directives.images import Image
@@ -221,6 +228,10 @@ def _option_format(arg):
221228
return directives.choice(arg, ('python', 'doctest'))
222229

223230

231+
def _option_string(arg):
232+
return arg if arg.strip() else False
233+
234+
224235
def mark_plot_labels(app, document):
225236
"""
226237
To make plots referenceable, we need to move the reference from the
@@ -271,7 +282,8 @@ class PlotDirective(Directive):
271282
'context': _option_context,
272283
'nofigs': directives.flag,
273284
'caption': directives.unchanged,
274-
}
285+
'filter-warning': _option_string,
286+
}
275287

276288
def run(self):
277289
"""Run the plot directive."""
@@ -307,6 +319,7 @@ def setup(app):
307319
app.add_config_value('plot_working_directory', None, True)
308320
app.add_config_value('plot_template', None, True)
309321
app.add_config_value('plot_srcset', [], True)
322+
app.add_config_value('plot_filter_warning', None, True)
310323
app.connect('doctree-read', mark_plot_labels)
311324
app.add_css_file('plot_directive.css')
312325
app.connect('build-finished', _copy_css_file)
@@ -854,16 +867,20 @@ def run(arguments, content, options, state_machine, state, lineno):
854867

855868
# make figures
856869
try:
857-
results = render_figures(code=code,
858-
code_path=source_file_name,
859-
output_dir=build_dir,
860-
output_base=output_base,
861-
context=keep_context,
862-
function_name=function_name,
863-
config=config,
864-
context_reset=context_opt == 'reset',
865-
close_figs=context_opt == 'close-figs',
866-
code_includes=source_file_includes)
870+
with warnings.catch_warnings(record=True) as w:
871+
if msg := options.get('filter-warning', False):
872+
warnings.filterwarnings('ignore', message=msg)
873+
874+
results = render_figures(code=code,
875+
code_path=source_file_name,
876+
output_dir=build_dir,
877+
output_base=output_base,
878+
context=keep_context,
879+
function_name=function_name,
880+
config=config,
881+
context_reset=context_opt == 'reset',
882+
close_figs=context_opt == 'close-figs',
883+
code_includes=source_file_includes)
867884
errors = []
868885
except PlotError as err:
869886
reporter = state.memo.reporter

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.