6060 changed using the ``plot_html_show_source_link`` variable in
6161 :file:`conf.py` (which itself defaults to True).
6262
63- ``:encoding:`` : str
64- If this source file is in a non-UTF8 or non-ASCII encoding, the
65- encoding must be specified using the ``:encoding:`` option. The
66- encoding will not be inferred using the ``-*- coding -*-`` metacomment.
67-
6863 ``:context:`` : bool or str
6964 If provided, the code will be run in the context of all previous plot
7065 directives for which the ``:context:`` option was specified. This only
166161import matplotlib
167162from matplotlib .backend_bases import FigureManagerBase
168163import matplotlib .pyplot as plt
169- from matplotlib import _api , _pylab_helpers , cbook
164+ from matplotlib import _pylab_helpers , cbook
170165
171166matplotlib .use ("agg" )
172167
@@ -200,11 +195,6 @@ def _option_format(arg):
200195 return directives .choice (arg , ('python' , 'doctest' ))
201196
202197
203- def _deprecated_option_encoding (arg ):
204- _api .warn_deprecated ("3.5" , name = "encoding" , obj_type = "option" )
205- return directives .encoding (arg )
206-
207-
208198def mark_plot_labels (app , document ):
209199 """
210200 To make plots referenceable, we need to move the reference from the
@@ -254,7 +244,6 @@ class PlotDirective(Directive):
254244 'format' : _option_format ,
255245 'context' : _option_context ,
256246 'nofigs' : directives .flag ,
257- 'encoding' : _deprecated_option_encoding ,
258247 'caption' : directives .unchanged ,
259248 }
260249
@@ -316,47 +305,25 @@ def contains_doctest(text):
316305 return bool (m )
317306
318307
319- @_api .deprecated ("3.5" , alternative = "doctest.script_from_examples" )
320- def unescape_doctest (text ):
321- """
322- Extract code from a piece of text, which contains either Python code
323- or doctests.
324- """
325- if not contains_doctest (text ):
326- return text
327- code = ""
328- for line in text .split ("\n " ):
329- m = re .match (r'^\s*(>>>|\.\.\.) (.*)$' , line )
330- if m :
331- code += m .group (2 ) + "\n "
332- elif line .strip ():
333- code += "# " + line .strip () + "\n "
334- else :
335- code += "\n "
336- return code
337-
338-
339- @_api .deprecated ("3.5" )
340- def split_code_at_show (text ):
308+ def _split_code_at_show (text , function_name ):
341309 """Split code at plt.show()."""
342- return _split_code_at_show (text )[1 ]
343-
344310
345- def _split_code_at_show (text ):
346- """Split code at plt.show()."""
347- parts = []
348311 is_doctest = contains_doctest (text )
349- part = []
350- for line in text .split ("\n " ):
351- if (not is_doctest and line .strip () == 'plt.show()' ) or \
352- (is_doctest and line .strip () == '>>> plt.show()' ):
353- part .append (line )
312+ if function_name is None :
313+ parts = []
314+ part = []
315+ for line in text .split ("\n " ):
316+ if ((not is_doctest and line .startswith ('plt.show(' )) or
317+ (is_doctest and line .strip () == '>>> plt.show()' )):
318+ part .append (line )
319+ parts .append ("\n " .join (part ))
320+ part = []
321+ else :
322+ part .append (line )
323+ if "\n " .join (part ).strip ():
354324 parts .append ("\n " .join (part ))
355- part = []
356- else :
357- part .append (line )
358- if "\n " .join (part ).strip ():
359- parts .append ("\n " .join (part ))
325+ else :
326+ parts = [text ]
360327 return is_doctest , parts
361328
362329
@@ -469,15 +436,6 @@ class PlotError(RuntimeError):
469436 pass
470437
471438
472- @_api .deprecated ("3.5" )
473- def run_code (code , code_path , ns = None , function_name = None ):
474- """
475- Import a Python module from a path, and run the function given by
476- name, if function_name is not None.
477- """
478- _run_code (unescape_doctest (code ), code_path , ns , function_name )
479-
480-
481439def _run_code (code , code_path , ns = None , function_name = None ):
482440 """
483441 Import a Python module from a path, and run the function given by
@@ -566,28 +524,30 @@ def render_figures(code, code_path, output_dir, output_base, context,
566524 Save the images under *output_dir* with file names derived from
567525 *output_base*
568526 """
527+ if function_name is not None :
528+ output_base = f'{ output_base } _{ function_name } '
569529 formats = get_plot_formats (config )
570530
571531 # Try to determine if all images already exist
572532
573- is_doctest , code_pieces = _split_code_at_show (code )
533+ is_doctest , code_pieces = _split_code_at_show (code , function_name )
574534
575535 # Look for single-figure output files first
576- all_exists = True
577536 img = ImageFile (output_base , output_dir )
578537 for format , dpi in formats :
579538 if context or out_of_date (code_path , img .filename (format ),
580539 includes = code_includes ):
581540 all_exists = False
582541 break
583542 img .formats .append (format )
543+ else :
544+ all_exists = True
584545
585546 if all_exists :
586547 return [(code , [img ])]
587548
588549 # Then look for multi-figure output files
589550 results = []
590- all_exists = True
591551 for i , code_piece in enumerate (code_pieces ):
592552 images = []
593553 for j in itertools .count ():
@@ -611,6 +571,8 @@ def render_figures(code, code_path, output_dir, output_base, context,
611571 if not all_exists :
612572 break
613573 results .append ((code_piece , images ))
574+ else :
575+ all_exists = True
614576
615577 if all_exists :
616578 return results
@@ -793,13 +755,13 @@ def run(arguments, content, options, state_machine, state, lineno):
793755
794756 # make figures
795757 try :
796- results = render_figures (code ,
797- source_file_name ,
798- build_dir ,
799- output_base ,
800- keep_context ,
801- function_name ,
802- config ,
758+ results = render_figures (code = code ,
759+ code_path = source_file_name ,
760+ output_dir = build_dir ,
761+ output_base = output_base ,
762+ context = keep_context ,
763+ function_name = function_name ,
764+ config = config ,
803765 context_reset = context_opt == 'reset' ,
804766 close_figs = context_opt == 'close-figs' ,
805767 code_includes = source_file_includes )
0 commit comments