Skip to content

Commit 426abc7

Browse files
committed
Remove {counter} from output-base-name and remove the global config
The idea of allowing the use of the counter for custom base names is flawed because the counter would always be incremented even for custom names that don't use it. Also, the global base name is difficult to get working because we don't have a good global view of the plots to create a counter, especially in the case of partial rebuilds. Instead, we just simplify things by just allowing setting a custom-base-name. If two plot directives use the same custom-base-name, then one will end up overwriting the other (I'm still looking into whether it's possible to at least detect this and give an error).
1 parent b432962 commit 426abc7

File tree

3 files changed

+11
-19
lines changed

3 files changed

+11
-19
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@
5050
``:output-base-name:`` : str
5151
The base name (without the extension) of the outputted image files. The
5252
default is to use the same name as the input script, or the name of
53-
the RST document if no script is provided. The string can include the
54-
format ``{counter}`` to use an incremented counter. For example,
55-
``'plot-{counter}'`` will create files like ``plot-1.png``, ``plot-2.png``,
56-
and so on. If the ``{counter}`` is not provided, two plots with the same
53+
the RST document if no script is provided. Note: two plots with the same
5754
output-base-name may overwrite each other.
5855
5956
``:format:`` : {'python', 'doctest'}
@@ -97,10 +94,6 @@
9794
9895
The plot directive has the following configuration options:
9996
100-
plot_output_base_name
101-
Default value for the output-base-name option (default is to use the name
102-
of the input script, or the name of the RST file if no script is provided)
103-
10497
plot_include_source
10598
Default value for the include-source option (default: False).
10699
@@ -313,7 +306,6 @@ def setup(app):
313306
app.add_config_value('plot_pre_code', None, True)
314307
app.add_config_value('plot_include_source', False, True)
315308
app.add_config_value('plot_html_show_source_link', True, True)
316-
app.add_config_value('plot_output_base_name', None, True)
317309
app.add_config_value('plot_formats', ['png', 'hires.png', 'pdf'], True)
318310
app.add_config_value('plot_basedir', None, True)
319311
app.add_config_value('plot_html_show_formats', True, True)
@@ -749,7 +741,7 @@ def run(arguments, content, options, state_machine, state, lineno):
749741

750742
options.setdefault('include-source', config.plot_include_source)
751743
options.setdefault('show-source-link', config.plot_html_show_source_link)
752-
options.setdefault('output-base-name', config.plot_output_base_name)
744+
options.setdefault('output-base-name', None)
753745

754746
if 'class' in options:
755747
# classes are parsed into a list of string, and output by simply
@@ -798,12 +790,12 @@ def run(arguments, content, options, state_machine, state, lineno):
798790
else:
799791
source_file_name = rst_file
800792
code = textwrap.dedent("\n".join(map(str, content)))
801-
counter = document.attributes.get('_plot_counter', 0) + 1
802-
document.attributes['_plot_counter'] = counter
803793
base, ext = os.path.splitext(os.path.basename(source_file_name))
804794
if options['output-base-name']:
805-
output_base = options['output-base-name'].format(counter=counter)
795+
output_base = options['output-base-name']
806796
else:
797+
counter = document.attributes.get('_plot_counter', 0) + 1
798+
document.attributes['_plot_counter'] = counter
807799
output_base = '%s-%d.py' % (base, counter)
808800
function_name = None
809801
caption = options.get('caption', '')

lib/matplotlib/tests/test_sphinxext.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ def plot_directive_file(num):
9797
assert filecmp.cmp(range_6, plot_file(17))
9898
# plot 22 is from the range6.py file again, but a different function
9999
assert filecmp.cmp(range_10, img_dir / 'range6_range10.png')
100-
# plots 23 and 24 use a custom base name with {counter}
101-
assert filecmp.cmp(range_4, img_dir / 'custom-base-name-18.png')
102-
assert filecmp.cmp(range_6, img_dir / 'custom-base-name-19.png')
100+
# plots 23 and 24 use a custom base name
101+
assert filecmp.cmp(range_4, img_dir / 'custom-base-name-1.png')
102+
assert filecmp.cmp(range_6, img_dir / 'custom-base-name-2.png')
103103

104104
# Modify the included plot
105105
contents = (tmp_path / 'included_plot_21.rst').read_bytes()

lib/matplotlib/tests/tinypages/some_plots.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,14 @@ Plot 22 uses a different specific function in a file with plot commands:
175175

176176
.. plot:: range6.py range10
177177

178-
Plots 23 and 24 use output-base-name with a {counter}.
178+
Plots 23 and 24 use output-base-name.
179179

180180
.. plot::
181-
:output-base-name: custom-base-name-{counter}
181+
:output-base-name: custom-base-name-1
182182

183183
plt.plot(range(4))
184184

185185
.. plot::
186-
:output-base-name: custom-base-name-{counter}
186+
:output-base-name: custom-base-name-2
187187

188188
plt.plot(range(6))

0 commit comments

Comments
 (0)