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

Skip to content

Commit cb4bba2

Browse files
committed
RF: always close old figure windows
The plot directive first runs the specified code, then collects figures with:: fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() If you are using the "context" option, prior figures maybe generated from a previous plot directive command, are still open, and therefore collected with the new figure(s). The old figures will usually be earlier in the list, and so, for something like this ReST block, you will always get the first generated figure (``range(10)``) repeated in the second plot directive panel:: ####### A title ####### .. plot:: :context: import matplotlib.pyplot as plt plt.plot(range(10)) Then some text. .. plot:: :context: plt.figure() plt.plot(range(5), 'r') This PR always closes previous figure windows before running the plot directive code. See email discussion thread starting here: https://www.mail-archive.com/[email protected]/msg10816.html
1 parent edd4f86 commit cb4bba2

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/matplotlib/sphinxext/plot_directive.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,9 @@ def render_figures(code, code_path, output_dir, output_base, context,
604604
for i, code_piece in enumerate(code_pieces):
605605

606606
if not context or config.plot_apply_rcparams:
607-
clear_state(config.plot_rcparams, close=not context)
607+
clear_state(config.plot_rcparams)
608+
else: # Always clear previous figures before running code
609+
plt.close('all')
608610

609611
run_code(code_piece, code_path, ns, function_name)
610612

0 commit comments

Comments
 (0)