Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DOC: plot-directive warning filter option #27076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions lib/matplotlib/sphinxext/plot_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
figure. This overwrites the caption given in the content, when the plot
is generated from a file.

``:filter-warning:`` : str
When specified, will ignore warnings that match the input string, which is
a warnings filter `message regex <msg>`_ string.

.. _msg: https://docs.python.org/3/library/warnings.html#the-warnings-filter

Additionally, this directive supports all the options of the `image directive
<https://docutils.sourceforge.io/docs/ref/rst/directives.html#image>`_,
except for ``:target:`` (since plot will add its own target). These include
Expand Down Expand Up @@ -177,6 +183,7 @@
import sys
import textwrap
import traceback
import warnings

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


def _option_string(arg):
return arg if arg.strip() else False


def mark_plot_labels(app, document):
"""
To make plots referenceable, we need to move the reference from the
Expand Down Expand Up @@ -271,7 +282,8 @@ class PlotDirective(Directive):
'context': _option_context,
'nofigs': directives.flag,
'caption': directives.unchanged,
}
'filter-warning': _option_string,
}

def run(self):
"""Run the plot directive."""
Expand Down Expand Up @@ -854,16 +866,20 @@ def run(arguments, content, options, state_machine, state, lineno):

# make figures
try:
results = render_figures(code=code,
code_path=source_file_name,
output_dir=build_dir,
output_base=output_base,
context=keep_context,
function_name=function_name,
config=config,
context_reset=context_opt == 'reset',
close_figs=context_opt == 'close-figs',
code_includes=source_file_includes)
with warnings.catch_warnings(record=True) as w:
if msg := options.get('filter-warning', False):
warnings.filterwarnings('ignore', message=msg)

results = render_figures(code=code,
code_path=source_file_name,
output_dir=build_dir,
output_base=output_base,
context=keep_context,
function_name=function_name,
config=config,
context_reset=context_opt == 'reset',
close_figs=context_opt == 'close-figs',
code_includes=source_file_includes)
errors = []
except PlotError as err:
reporter = state.memo.reporter
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/tinypages/some_plots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,11 @@ Plot 21 is generated via an include directive:
Plot 22 uses a different specific function in a file with plot commands:

.. plot:: range6.py range10

Plot 23 filters a missing glyph warning

.. plot::
:filter-warning: Glyph

plt.text(.5, .5, "Hello 🙃 World!")