def makefig(code, code_path, output_dir, output_base, config):
<EOL>default_dpi = {'<STR_LIT>': <NUM_LIT>, '<STR_LIT>': <NUM_LIT:200>, '<STR_LIT>': <NUM_LIT:50>}<EOL>formats = []<EOL>for fmt in config.plot_formats:<EOL><INDENT>if isinstance(fmt, str):<EOL><INDENT>formats.append((fmt, default_dpi.get(fmt, <NUM_LIT>)))<EOL><DEDENT>elif type(fmt) in (tuple, list) and len(fmt)==<NUM_LIT:2>:<EOL><INDENT>formats.append((str(fmt[<NUM_LIT:0>]), int(fmt[<NUM_LIT:1>])))<EOL><DEDENT>else:<EOL><INDENT>raise PlotError('<STR_LIT>' % fmt)<EOL><DEDENT><DEDENT>code_pieces = split_code_at_show(code)<EOL>all_exists = True<EOL>img = ImageFile(output_base, output_dir)<EOL>for format, dpi in formats:<EOL><INDENT>if out_of_date(code_path, img.filename(format)):<EOL><INDENT>all_exists = False<EOL>break<EOL><DEDENT>img.formats.append(format)<EOL><DEDENT>if all_exists:<EOL><INDENT>return [(code, [img])]<EOL><DEDENT>results = []<EOL>all_exists = True<EOL>for i, code_piece in enumerate(code_pieces):<EOL><INDENT>images = []<EOL>for j in range(<NUM_LIT:1000>):<EOL><INDENT>img = ImageFile('<STR_LIT>' % (output_base, i, j), output_dir)<EOL>for format, dpi in formats:<EOL><INDENT>if out_of_date(code_path, img.filename(format)):<EOL><INDENT>all_exists = False<EOL>break<EOL><DEDENT>img.formats.append(format)<EOL><DEDENT>if not all_exists:<EOL><INDENT>all_exists = (j > <NUM_LIT:0>)<EOL>break<EOL><DEDENT>images.append(img)<EOL><DEDENT>if not all_exists:<EOL><INDENT>break<EOL><DEDENT>results.append((code_piece, images))<EOL><DEDENT>if all_exists:<EOL><INDENT>return results<EOL><DEDENT>results = []<EOL>ns = {}<EOL>for i, code_piece in enumerate(code_pieces):<EOL><INDENT>plt.close('<STR_LIT:all>')<EOL>run_code(code_piece, code_path, ns)<EOL>images = []<EOL>fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()<EOL>for j, figman in enumerate(fig_managers):<EOL><INDENT>if len(fig_managers) == <NUM_LIT:1> and len(code_pieces) == <NUM_LIT:1>:<EOL><INDENT>img = ImageFile(output_base, output_dir)<EOL><DEDENT>else:<EOL><INDENT>img = ImageFile("<STR_LIT>" % (output_base, i, j),<EOL>output_dir)<EOL><DEDENT>images.append(img)<EOL>for format, dpi in formats:<EOL><INDENT>try:<EOL><INDENT>figman.canvas.figure.savefig(img.filename(format), dpi=dpi)<EOL><DEDENT>except exceptions.BaseException as err:<EOL><INDENT>raise PlotError(traceback.format_exc())<EOL><DEDENT>img.formats.append(format)<EOL><DEDENT><DEDENT>results.append((code_piece, images))<EOL><DEDENT>return results<EOL>
Run a pyplot script *code* and save the images under *output_dir*
with file names derived from *output_base*