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

Skip to content

Commit 90069b4

Browse files
author
Ulrich J. Herter
committed
plot directive: caption-option
This new option allows the user to add a caption to plots with the plotting code given in the directive's contents. * The option :caption: enforces a caption. * The option take precedence over the contents. * Tested and documented in the modul's doc string. * Note in next_whats_new
1 parent 2c265ed commit 90069b4

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 10 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,6 +245,7 @@ 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):
@@ -633,8 +639,9 @@ def run(arguments, content, options, state_machine, state, lineno):
633639
source_file_name = os.path.join(setup.confdir, config.plot_basedir,
634640
directives.uri(arguments[0]))
635641

636-
# If there is content, it will be passed as a caption.
637-
caption = '\n'.join(content)
642+
# If there is content, it will be passed as a caption, if the caption
643+
# option is not present.
644+
caption = options.get("caption", '\n'.join(content))
638645

639646
# If the optional function name is provided, use it
640647
if len(arguments) == 2:
@@ -652,7 +659,7 @@ def run(arguments, content, options, state_machine, state, lineno):
652659
base, ext = os.path.splitext(os.path.basename(source_file_name))
653660
output_base = '%s-%d.py' % (base, counter)
654661
function_name = None
655-
caption = ''
662+
caption = options.get('caption', '')
656663

657664
base, source_ext = os.path.splitext(output_base)
658665
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 usin :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 actual caption for plot 18.' in html_contents

lib/matplotlib/tests/tinypages/some_plots.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,19 @@ 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+
the using :caption: option:
141+
142+
.. plot:: range4.py
143+
:caption: This is the actual caption for plot 18.
144+
145+
This is the caption for plot 18.

0 commit comments

Comments
 (0)