8000 Error in the plot directive if the plot file already exists · matplotlib/matplotlib@4df7c14 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4df7c14

Browse files
committed
Error in the plot directive if the plot file already exists
Previously one plot would just silently overwrite the other, leading to confusing output where both directives show the plot from the one that was executed last.
1 parent 13f7c7b commit 4df7c14

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
the RST document if no script is provided. The string can include the
5454
format ``{counter}`` to use an incremented counter. For example,
5555
``'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
57-
output-base-name may overwrite each other.
56+
and so on.
5857
5958
``:format:`` : {'python', 'doctest'}
6059
The format of the input. If unset, the format is auto-detected.
@@ -98,8 +97,11 @@
9897
The plot directive has the following configuration options:
9998
10099
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)
100+
Default value for the output-base-name option. It's recommended to the
101+
format string ``{counter}`` (for example, ``'plot-{counter}'``) so that
102+
multiple plots in the same document will not conflict with each other.
103+
(default is to use the name of the input script, or the name of the RST
104+
file if no script is provided)
103105
104106
plot_include_source
105107
Default value for the include-source option (default: False).
@@ -714,13 +716,17 @@ def render_figures(code, code_path, output_dir, output_base, context,
714716

715717
for fmt, dpi in formats:
716718
try:
719+
if os.path.exists(img.filename(fmt)):
720+
raise PlotError('file %s already exists. You may need to set output-base-name to something else.' % img.filename(fmt))
717721
figman.canvas.figure.savefig(img.filename(fmt), dpi=dpi)
718722
if fmt == formats[0][0] and config.plot_srcset:
719723
# save a 2x, 3x etc version of the default...
720724
srcset = _parse_srcset(config.plot_srcset)
721725
for mult, suffix in srcset.items():
722726
fm = f'{suffix}.{fmt}'
723727
img.formats.append(fm)
728+
if os.path.exists(img.filename(fm)):
729+
raise PlotError('file %s already exists. You may need to set output-base-name to something else.' % img.filename(fmt))
724730
figman.canvas.figure.savefig(img.filename(fm),
725731
dpi=int(dpi * mult))
726732
except Exception as err:

0 commit comments

Comments
 (0)
0