def save(self, name, mpl_kwargs=None, canvas=None): """Choose backend and save image to disk""" from ._mpl_imports import \ FigureCanvasAgg, FigureCanvasPdf, FigureCanvasPS if mpl_kwargs is None: mpl_kwargs = {} if 'papertype' not in mpl_kwargs: mpl_kwargs['papertype'] = 'auto' suffix = get_image_suffix(name) if suffix == '': suffix = '.png' name = "%s%s" % (name, suffix) mylog.info("Saving plot %s", name) if suffix == ".png": canvas = FigureCanvasAgg(self.figure) elif suffix == ".pdf": canvas = FigureCanvasPdf(self.figure) elif suffix in (".eps", ".ps"): canvas = FigureCanvasPS(self.figure) else: mylog.warning("Unknown suffix %s, defaulting to Agg", suffix) canvas = self.canvas with matplotlib_style_context(): canvas.print_figure(name, **mpl_kwargs) return name
def _repr_png_(self): from ._mpl_imports import FigureCanvasAgg canvas = FigureCanvasAgg(self.figure) f = BytesIO() with matplotlib_style_context(): canvas.print_figure(f) f.seek(0) return f.read()
def save(self, name=None, suffix=None, mpl_kwargs=None): r""" Saves a 1d profile plot. Parameters ---------- name : str The output file keyword. suffix : string Specify the image type by its suffix. If not specified, the output type will be inferred from the filename. Defaults to PNG. mpl_kwargs : dict A dict of keyword arguments to be passed to matplotlib. """ if not self._plot_valid: self._setup_plots() unique = set(self.plots.values()) if len(unique) < len(self.plots): iters = izip(range(len(unique)), sorted(unique)) else: iters = iteritems(self.plots) if not suffix: suffix = "png" suffix = ".%s" % suffix fullname = False if name is None: if len(self.profiles) == 1: prefix = self.profiles[0].ds else: prefix = "Multi-data" name = "%s%s" % (prefix, suffix) else: sfx = get_image_suffix(name) if sfx != '': suffix = sfx prefix = name[:name.rfind(suffix)] fullname = True else: prefix = name xfn = self.profiles[0].x_field if isinstance(xfn, tuple): xfn = xfn[1] fns = [] for uid, plot in iters: if isinstance(uid, tuple): uid = uid[1] if fullname: fns.append("%s%s" % (prefix, suffix)) else: fns.append("%s_1d-Profile_%s_%s%s" % (prefix, xfn, uid, suffix)) mylog.info("Saving %s", fns[-1]) with matplotlib_style_context(): plot.save(fns[-1], mpl_kwargs=mpl_kwargs) return fns
def _repr_html_(self): """Return an html representation of the plot object. Will display as a png for each WindowPlotMPL instance in self.plots""" ret = '' unique = set(self.plots.values()) if len(unique) < len(self.plots): iters = izip(range(len(unique)), sorted(unique)) else: iters = iteritems(self.plots) for uid, plot in iters: with matplotlib_style_context(): img = plot._repr_png_() img = base64.b64encode(img).decode() ret += r'<img style="max-width:100%%;max-height:100%%;" ' \ r'src="data:image/png;base64,{0}"><br>'.format(img) return ret
def save(self, name, mpl_kwargs=None, canvas=None): """Choose backend and save image to disk""" if mpl_kwargs is None: mpl_kwargs = {} if "papertype" not in mpl_kwargs and Version( matplotlib.__version__) < Version("3.3.0"): mpl_kwargs["papertype"] = "auto" name = validate_image_name(name) try: canvas = get_canvas(self.figure, name) except ValueError: canvas = self.canvas mylog.info("Saving plot %s", name) with matplotlib_style_context(): canvas.print_figure(name, **mpl_kwargs) return name
def save(self, name, mpl_kwargs=None, canvas=None): """Choose backend and save image to disk""" from ._mpl_imports import ( FigureCanvasAgg, FigureCanvasPdf, FigureCanvasPS, FigureCanvasSVG, ) if mpl_kwargs is None: mpl_kwargs = {} if "papertype" not in mpl_kwargs and LooseVersion( matplotlib.__version__ ) < LooseVersion("3.3.0"): mpl_kwargs["papertype"] = "auto" suffix = get_image_suffix(name) if suffix == "": suffix = ".png" name = f"{name}{suffix}" mylog.info("Saving plot %s", name) if suffix in _AGG_FORMATS: canvas = FigureCanvasAgg(self.figure) elif suffix in (".svg", ".svgz"): canvas = FigureCanvasSVG(self.figure) elif suffix == ".pdf": canvas = FigureCanvasPdf(self.figure) elif suffix in (".eps", ".ps"): canvas = FigureCanvasPS(self.figure) else: mylog.warning("Unknown suffix %s, defaulting to Agg", suffix) canvas = self.canvas with matplotlib_style_context(): canvas.print_figure(name, **mpl_kwargs) return name