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

Skip to content

Commit 33affe1

Browse files
authored
Merge pull request matplotlib#18426 from ulijh/plot-directive-caption-option
plot directive: caption-option
2 parents 5729cb5 + c6ab4ab commit 33affe1

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
If specified, the code block will be run, but no figures will be
7272
inserted. This is usually useful with the ``:context:`` option.
7373
74+
caption : str
75+
If specified, the option's argument will be used as a caption for the
76+
figure. This overwrites the caption given in the content, when the plot
77+
is generated from a file.
78+
7479
Additionally, this directive supports all of the options of the `image`
7580
directive, except for *target* (since plot will add its own target). These
7681
include *alt*, *height*, *width*, *scale*, *align* and *class*.
@@ -240,12 +245,16 @@ class PlotDirective(Directive):
240245
'context': _option_context,
241246
'nofigs': directives.flag,
242247
'encoding': directives.encoding,
248+
'caption': directives.unchanged,
243249
}
244250

245251
def run(self):
246252
"""Run the plot directive."""
247-
return run(self.arguments, self.content, self.options,
248-
self.state_machine, self.state, self.lineno)
253+
try:
254+
return run(self.arguments, self.content, self.options,
255+
self.state_machine, self.state, self.lineno)
256+
except Exception as e:
257+
raise self.error(str(e))
249258

250259

251260
def setup(app):
@@ -636,6 +645,16 @@ def run(arguments, content, options, state_machine, state, lineno):
636645
# If there is content, it will be passed as a caption.
637646
caption = '\n'.join(content)
638647

648+
# Enforce unambiguous use of captions.
649+
if "caption" in options:
650+
if caption:
651+
raise ValueError(
652+
'Caption specified in both content and options.'
653+
' Please remove ambiguity.'
654+
)
655+
# Use caption option
656+
caption = options["caption"]
657+
639658
# If the optional function name is provided, use it
640659
if len(arguments) == 2:
641660
function_name = arguments[1]
@@ -652,7 +671,7 @@ def run(arguments, content, options, state_machine, state, lineno):
652671
base, ext = os.path.splitext(os.path.basename(source_file_name))
653672
output_base = '%s-%d.py' % (base, counter)
654673
function_name = None
655-
caption = ''
674+
caption = options.get('caption', '')
656675

657676
base, source_ext = os.path.splitext(output_base)
658677
if source_ext in ('.py', '.rst', '.txt'):

lib/matplotlib/tests/test_sphinxext.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def test_tinypages(tmpdir):
2121
str(Path(__file__).parent / 'tinypages'), str(html_dir)]
2222
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
2323
out, err = proc.communicate()
24+
2425
assert proc.returncode == 0, \
2526
"sphinx build failed with stdout:\n{}\nstderr:\n{}\n".format(out, err)
2627
if err:
@@ -52,3 +53,7 @@ def plot_file(num):
5253
assert filecmp.cmp(range_6, html_dir / 'range6.png')
5354
# check if figure caption made it into html file
5455
assert b'This is the caption for plot 15.' in html_contents
56+
# check if figure caption using :caption: made it into html file
57+
assert b'Plot 17 uses the caption option.' in html_contents
58+
# check if figure caption made it into html file
59+
assert b'This is the caption for plot 18.' in html_contents

lib/matplotlib/tests/tinypages/some_plots.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,17 @@ Plot 16 uses a specific function in a file with plot commands:
127127
.. plot:: range6.py range6
128128

129129

130+
Plot 17 gets a caption specified by the :caption: option:
131+
132+
.. plot::
133+
:caption: Plot 17 uses the caption option.
134+
135+
plt.figure()
136+
plt.plot(range(6))
137+
138+
139+
Plot 18 uses an external file with the plot commands and a caption
140+
using the :caption: option:
141+
142+
.. plot:: range4.py
143+
:caption: This is the caption for plot 18.

0 commit comments

Comments
 (0)