|
62 | 62 | If provided, the code will be run in the context of all
|
63 | 63 | previous plot directives for which the `:context:` option was
|
64 | 64 | specified. This only applies to inline code plot directives,
|
65 |
| - not those run from files. If the ``:context: reset`` is specified, |
66 |
| - the context is reset for this and future plots. |
| 65 | + not those run from files. If the ``:context: reset`` option is |
| 66 | + specified, the context is reset for this and future plots, and |
| 67 | + previous figures are closed prior to running the code. |
| 68 | + ``:context:close-figs`` keeps the context but closes previous figures |
| 69 | + before running the code. |
67 | 70 |
|
68 | 71 | nofigs : bool
|
69 | 72 | If specified, the code block will be run, but no figures will
|
@@ -190,11 +193,9 @@ def _option_boolean(arg):
|
190 | 193 |
|
191 | 194 |
|
192 | 195 | def _option_context(arg):
|
193 |
| - if arg in [None, 'reset']: |
| 196 | + if arg in [None, 'reset', 'close-figs']: |
194 | 197 | return arg
|
195 |
| - else: |
196 |
| - raise ValueError("argument should be None or 'reset'") |
197 |
| - return directives.choice(arg, ('None', 'reset')) |
| 198 | + raise ValueError("argument should be None or 'reset' or 'close-figs'") |
198 | 199 |
|
199 | 200 |
|
200 | 201 | def _option_format(arg):
|
@@ -333,8 +334,8 @@ def remove_coding(text):
|
333 | 334 | """
|
334 | 335 | Remove the coding comment, which six.exec_ doesn't like.
|
335 | 336 | """
|
336 |
| - return re.sub( |
337 |
| - "^#\s*-\*-\s*coding:\s*.*-\*-$", "", text, flags=re.MULTILINE) |
| 337 | + sub_re = re.compile("^#\s*-\*-\s*coding:\s*.*-\*-$", flags=re.MULTILINE) |
| 338 | + return sub_re.sub("", text) |
338 | 339 |
|
339 | 340 | #------------------------------------------------------------------------------
|
340 | 341 | # Template
|
@@ -524,7 +525,8 @@ def clear_state(plot_rcparams, close=True):
|
524 | 525 |
|
525 | 526 |
|
526 | 527 | def render_figures(code, code_path, output_dir, output_base, context,
|
527 |
| - function_name, config, context_reset=False): |
| 528 | + function_name, config, context_reset=False, |
| 529 | + close_figs=False): |
528 | 530 | """
|
529 | 531 | Run a pyplot script and save the low and high res PNGs and a PDF
|
530 | 532 | in *output_dir*.
|
@@ -600,11 +602,16 @@ def render_figures(code, code_path, output_dir, output_base, context,
|
600 | 602 |
|
601 | 603 | if context_reset:
|
602 | 604 | clear_state(config.plot_rcparams)
|
| 605 | + plot_context.clear() |
| 606 | + |
| 607 | + close_figs = not context or close_figs |
603 | 608 |
|
604 | 609 | for i, code_piece in enumerate(code_pieces):
|
605 | 610 |
|
606 | 611 | if not context or config.plot_apply_rcparams:
|
607 |
| - clear_state(config.plot_rcparams, close=not context) |
| 612 | + clear_state(config.plot_rcparams, close_figs) |
| 613 | + elif close_figs: |
| 614 | + plt.close('all') |
608 | 615 |
|
609 | 616 | run_code(code_piece, code_path, ns, function_name)
|
610 | 617 |
|
@@ -644,8 +651,8 @@ def run(arguments, content, options, state_machine, state, lineno):
|
644 | 651 | nofigs = 'nofigs' in options
|
645 | 652 |
|
646 | 653 | options.setdefault('include-source', config.plot_include_source)
|
647 |
| - context = 'context' in options |
648 |
| - context_reset = True if (context and options['context'] == 'reset') else False |
| 654 | + keep_context = 'context' in options |
| 655 | + context_opt = None if not keep_context else options['context'] |
649 | 656 |
|
650 | 657 | rst_file = document.attributes['source']
|
651 | 658 | rst_dir = os.path.dirname(rst_file)
|
@@ -729,9 +736,15 @@ def run(arguments, content, options, state_machine, state, lineno):
|
729 | 736 |
|
730 | 737 | # make figures
|
731 | 738 | try:
|
732 |
| - results = render_figures(code, source_file_name, build_dir, output_base, |
733 |
| - context, function_name, config, |
734 |
| - context_reset=context_reset) |
| 739 | + results = render_figures(code, |
| 740 | + source_file_name, |
| 741 | + build_dir, |
| 742 | + output_base, |
| 743 | + keep_context, |
| 744 | + function_name, |
| 745 | + config, |
| 746 | + context_reset=context_opt == 'reset', |
| 747 | + close_figs=context_opt == 'close-figs') |
735 | 748 | errors = []
|
736 | 749 | except PlotError as err:
|
737 | 750 | reporter = state.memo.reporter
|
|
0 commit comments