def plot_roc(roc: ROC, title: str = "", label: str = "", show=False, save=False, fig: plt.Figure = None): if fig is None: fig = plt.figure() ax = fig.add_axes([0.1, 0.1, 0.85, 0.8]) else: ax = fig.get_axes() assert len(ax) == 1 ax = ax[0] lw = 2 fpr, tpr, _ = roc ax.plot(fpr, tpr, lw=lw, label=label) ax.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--') ax.set(xlabel='FPR', ylabel='TPR', ylim=[0.0, 1.05], xlim=[0.0, 1.0]) ax.set_title(title, fontsize=15) ax.legend(loc="lower right") # fig.tight_layout() if show: fig.show() if save: fig.savefig(f'figs/{title}_{label}.png') return fig
def log_plt_figure(fig: plt.Figure, path: str) -> None: """ Log a matplotlib figure as an artifact. Parameters ---------- fig : matplotlib.pyplot.Figure Figure to log. path : str Path in the artifact store. Returns ------- None None Examples -------- >>> with mlflow.start_run() as run: ... fig, ax = plt.subplots() ... _ = ax.plot([0, 1], [0, 1]) ... mlflow.log_figure(fig, 'plt_figure.png') >>> list_artifacts(run.info.run_id) ['plt_figure.png'] """ with _artifact_context(path) as tmp_path: fig.savefig(tmp_path) plt.close(fig)
def create_thumbnail(infile, thumbfile, width=180, height=180, xoffset=0, yoffset=0, zoom=1, cx=0.5, cy=0.6, dpi=100): from matplotlib import image from matplotlib.pyplot import Figure baseout, extout = os.path.splitext(thumbfile) im = image.imread(infile) thumb = padded_thumbnail(im, (height, width), (yoffset, xoffset), zoom) extension = extout.lower() if extension == '.png': from matplotlib.backends.backend_agg \ import FigureCanvasAgg as FigureCanvas elif extension == '.pdf': from matplotlib.backends.backend_pdf \ import FigureCanvasPDF as FigureCanvas elif extension == '.svg': from matplotlib.backends.backend_svg \ import FigureCanvasSVG as FigureCanvas else: raise ValueError("Can only handle extensions 'png', 'svg' or 'pdf'") fig = Figure(figsize=(float(width) / dpi, float(height) / dpi), dpi=dpi) canvas = FigureCanvas(fig) ax = fig.add_axes([0, 0, 1, 1], aspect='auto', frameon=False, xticks=[], yticks=[]) ax.imshow(thumb, aspect='auto', resample=True, interpolation='bilinear') fig.savefig(thumbfile, dpi=dpi) return fig
def export( fig: plt.Figure, filename: Union[str, os.fspath], target_dir: str = "plots/", file_formats: Tuple[str] = (".pdf", ".png") ) -> None: """ Convenience function for saving a matplotlib figure. :param fig: A matplotlib figure. :param filename: Filename of the plot without .pdf suffix. :param file_formats: Tuple of file formats specifying the format figure will be saved as. :param target_dir: Directory where the plot will be saved in. Default is './plots/'. :return: None example: export(fig, 'simple', 'examples') """ if not os.path.isdir(target_dir): os.makedirs(target_dir) for file_format in file_formats: fig.savefig(os.path.join(target_dir, f'{filename}{file_format}'), bbox_inches="tight")
def save_fig(self, fig: plt.Figure, name: str, save: bool): _dir = self.run_viz_dir if self.opts else self.general_viz_dir if save: fig.savefig(_dir / name, bbox_inches='tight', dpi=120, transparent=True)
def export(fig: plt.Figure, filename: Union[AnyStr, os.fspath], target_dir: Union[AnyStr, os.fspath] = "plots/", file_formats: Tuple[str, ...] = (".pdf", ".png"), save_as_tikz: bool = False, close_figure: bool = False) -> None: """ Convenience function for saving a matplotlib figure. :param fig: A matplotlib figure. :param filename: Filename of the plot without .pdf suffix. :param file_formats: Tuple of file formats specifying the format figure will be saved as. :param target_dir: Directory where the plot will be saved in. Default is './plots/'. :param save_as_tikz: Save the plot also as raw tikz tex document. :param close_figure: Whether to close the figure after saving it. Default is False :return: None """ os.makedirs(target_dir, exist_ok=True) for file_format in file_formats: fig.savefig(os.path.join(target_dir, f"{filename}{file_format}"), bbox_inches="tight") if save_as_tikz: save_figure_as_tikz_tex_file(fig=fig, target_path=os.path.join( target_dir, f"{filename}_tikz.tex")) if close_figure: plt.close(fig) fig.clf()
def fig_to_b64(fig: plt.Figure): f = io.BytesIO() fig.savefig(f, format="png") f.seek(0) b64 = "data:image/png;base64," + base64.b64encode(f.read()).decode() f.close() return b64
def fromMatplotlib(cls, fig: plt.Figure): bio = BytesIO() fig.savefig(bio, format="png", bbox_inches="tight") bio.seek(0) s = base64.b64encode(bio.getvalue()).decode("utf-8").replace("\n", "") plt.close(fig) return Image(s)
def hist_save_plot(function_name: str, figure: plt.Figure, year: str, interval: str) -> None: """Saves plot in png files. Saves the plot generated in the functions of the hist_data_plot_matrices_trade module in png files. :param function_name: name of the function that generates the plot. :param figure: figure object that is going to be save. :param year: string of the year to be analyzed (i.e '2016'). :param interval: string of the interval to be analyzed (i.e. 'week', 'month', 'quarter', 'year') :return: None -- The function save the plot in a file and does not return a value. """ # Saving plot data if (not os.path.isdir( f'../../hist_plot/eigenvectors_physical_{year}/{function_name}/')): try: os.mkdir(f'../../hist_plot/eigenvectors_physical_{year}/' + f'{function_name}/') print('Folder to save data created') except FileExistsError: print('Folder exists. The folder was not created') figure.savefig(f'../../hist_plot/eigenvectors_physical_{year}' + f'/{function_name}/{function_name}' + f'_{year}_{interval}.png') print('Plot saved') print()
def _handle_plot_mode( mode: str, fig: Figure, axes: Axes, outfile: Path = None, save_dpi: int = None ) -> Union[Optional[PlotResult], Axes]: """Handle the various combinations of plotting arguments.""" if mode == "block": plt.show(block=True) elif mode == "noblock": plt.show(block=False) plt.pause(0.001) elif mode == "test": plt.show(block=False) plt.close(fig) elif mode == "save": if outfile is None: raise ValueError("Path not specified for `outfile`.") try: outfile = Path(outfile) except BaseException as e: raise ValueError("Cannot interpret outfile path.") from e make_parent_directories(outfile) print(f"Saving figure to {outfile}") fig.savefig(outfile, dpi=save_dpi) plt.close(fig) elif mode == "return": return fig, axes else: raise ValueError("Invalid plotting mode.") return None
def mpl_figure_to_rgba(figure: plt.Figure) -> np.ndarray: """Returns a `NumPy` array (RGBA image) from a `matplotlib` figure. Args: figure: The `matplotlib` figure to be converted to an RGBA image. Returns: The RGBA image. """ # Stores figure temporarily. with tempfile.NamedTemporaryFile(delete=True) as tmp: figure.savefig( "{}".format(tmp.name), format="png", bbox_inches="tight", pad_inches=0, transparent=True, ) # Reads figure on memory. image = imageio.imread("{}".format(tmp.name)) # Processes the image. image = image.astype(np.float32) / 255.0 return image
def save_plot( figure: plt.Figure, function_name: str, dates: List[str], time_step: str ) -> None: """Saves plot in png files. Saves the plot generated in the functions of the exact_distributions_correlation_analysis module in png files. :param figure: figure object that is going to be save. :param function_name: name of the function that generates the plot. :param dates: List of the interval of dates to be analyzed (i.e. ['1980-01', '2020-12']). :param time_step: time step of the data (i.e. '1m', '2m', '5m', ...). :return: None -- The function save the plot in a file and does not return a value. """ # Saving plot data figure.savefig( f"../plot/exact_distributions_correlation/{function_name}" + f"_{dates[0]}_{dates[1]}_step_{time_step}.png" ) print("Plot Saved") print()
def hist_save_plot(function_name: str, figure: plt.Figure, fx_pair: str, year: str) -> None: """Saves plot in png files. Saves the plot generated in the functions of the hist_data_plot_avg_responses_physical module in png files. :param function_name: name of the function that generates the plot. :param figure: figure object that is going to be save. :param fx_pair: string of the abbreviation of the forex pair to be analyzed :param year: string of the year to be analyzed (i.e '2016'). :return: None -- The function save the plot in a file and does not return a value. """ # Saving plot data if (not os.path.isdir(f'../../hist_plot/avg_responses_plot_{year}' + f'/{function_name}/')): try: os.mkdir(f'../../hist_plot/avg_responses_plot_{year}/' + f'{function_name}/') print('Folder to save data created') except FileExistsError: print('Folder exists. The folder was not created') figure.savefig( f'../../hist_plot/avg_responses_plot_{year}/{function_name}' + f'/{function_name}_{year}_{fx_pair}.png') print('Plot saved') print()
def save_matplotlib_figure(data: plt.Figure, file_name: str, saved_format: str = "png") -> BytesIO: """ This function saves a matplotlib Figure object to a file :param data: matplotlib Figure object containing the plot :param directory_path: str path to the directory without the ending slash (/) :param file_name: name of the file (without the extension) :param saved_format: str type of plot image to be saved, png or pdf, defaults to pdf (vector format) :return file_path, file_io: str file_path to the saved file, BytesIO containing the requested plot """ if saved_format == "png" or saved_format == "pdf": file_io = BytesIO() data.savefig(file_io, transparent=True, bbox_inches='tight', format=saved_format) return file_io else: raise ValueError("Expected png or pdf as saved_format")
def save_plot( figure: plt.Figure, function_name: str, dates: List[str], time_step: str, window: str, K_value: str, ) -> None: """Saves plot in png files. Saves the plot generated in the functions of the epochs_analysis module in png files. :param figure: figure object that is going to be save. :param function_name: name of the function that generates the plot. :param dates: List of the interval of dates to be analyzed (i.e. ['1980-01-01', '2020-12-31']). :param time_step: time step of the data (i.e. '1m', '1h', '1d', '1wk', '1mo'). :param window: window time to compute the volatility (i.e. '25'). :param K_value: number of companies to be used (i.e. '80', 'all'). :return: None -- The function save the plot in a file and does not return a value. """ # Saving plot data figure.savefig(f"../plot/epochs/{function_name}_{dates[0]}_{dates[1]}" + f"_step_{time_step}_win_{window}_K_{K_value}.png") print("Plot Saved") print()
def export(fig: plt.Figure, filename: Union[str, os.fspath], target_dir: str = "plots/", file_formats: Tuple[str] = (".pdf", ".png"), close_figure: bool = False) -> None: """ Convenience function for saving a matplotlib figure. :param fig: A matplotlib figure. :param filename: Filename of the plot without .pdf suffix. :param file_formats: Tuple of file formats specifying the format figure will be saved as. :param target_dir: Directory where the plot will be saved in. Default is './plots/'. :param close_figure: Whether to close the figure after saving it. Default is False :return: None """ os.makedirs(target_dir, exist_ok=True) for file_format in file_formats: fig.savefig(os.path.join(target_dir, f'{filename}{file_format}'), bbox_inches="tight") if close_figure: plt.close(fig)
def save_plot(directory: str, filename: str, figure: plt.Figure = None, date: str = "", create_date_dir: bool = True, extension: list = ["pdf", "png"], dpi=300): """ :param directory: where the plot is stored :param filename: should be of format 'myplot_{}.{}', where the first placeholder is used for the date and the second for the extension, or 'myplot.{}', or 'myplot.extension' :param figure: a specific figure to save. If None, save the last plot that has been drawn :param date: date that should appear in the plot filename :param create_date_dir: if True, create a sub-folder with the date :param extension: list of extension used to store the plot """ output_folder = os.path.join( directory, date) if create_date_dir and date else directory if not os.path.exists(output_folder): Path(output_folder).mkdir(parents=True, exist_ok=True) for e in extension: if figure: figure.savefig(os.path.join( output_folder, filename.format(date, e) if date else filename.format(e)), dpi=dpi) else: # Save the current plot; plt.savefig(os.path.join( output_folder, filename.format(date, e) if date else filename.format(e)), dpi=dpi)
def save_fig(fname, fig: plt.Figure = None, fpath=path.join("..", "figures")): if not path.exists(fpath): os.makedirs(fpath) to = path.join(fpath, fname) if fig is not None: fig.savefig(to) return plt.savefig(to)
def create_file_from_figure(fig: plt.Figure, file_name: str) -> File: """Create a Discord file containing the figure.""" history_plot = io.BytesIO() fig.savefig(history_plot, format="png") history_plot.seek(0) plt.close(fig) return File(history_plot, file_name)
def fig2img(fig: plt.Figure, dpi: int = 72) -> np.ndarray: """Convert a Matplotlib figure to a PIL Image and return it""" import io buf = io.BytesIO() fig.savefig(buf, dpi=dpi, bbox_inches="tight") buf.seek(0) return imread(buf)
def save(fig: plt.Figure, filename: str): """ Saves a matplotlib Figure to a file. It overwrites existing files with the same filename. Args: fig: matplotlib.pyplot.Figure filename: str """ fig.savefig(Path(_OUTPUT_PATH).resolve() / Path(filename))
def _save_and_show(self, fig: plt.Figure) -> None: """ Save and plot a figure. """ if self.mode == 'save' or self.mode == 'both': filename = self.filename + '.' + self.extension self._save_path = join(self._folder, self.subfolder, filename) fig.savefig(self._save_path) if self.mode == 'show' or self.mode == 'both': plt.show()
def _save_plot(figure: plotlib.Figure, path: str): """ Saves a plot to a given destination relative to the project """ # if we want to create the file to a non-existent directory make one parent_dir = os.path.abspath(os.path.join(path, os.pardir)) if not os.path.exists(parent_dir): os.makedirs(parent_dir, exist_ok=True) figure.savefig(path)
def _default_after_plots(self, fig: plt.Figure): """Set properties after charts creation Args: fig: matplotlib Figure """ fig.tight_layout() if self.figpath is not None: fig.savefig(self.figpath.format(i=self.file_idx)) self.file_idx += 1 plt.show()
def add_figure(self, figure: plt.Figure, caption: Optional[str] = None): buf = io.BytesIO() figure.set_size_inches(6, 4) figure.set_dpi(300) figure.savefig(buf, format="png") b64url = "data:image/png;base64,%s" % base64.b64encode( buf.getvalue()).decode("utf8") self.__elements.append({"kind": "figure", "url": b64url}) plt.clf()
def save_fig(fig: plt.Figure, save_path: Path, create_dirs: bool = False, **kwargs) -> None: """Save a figure and potentially create the location. """ if not save_path.parent.exists(): if create_dirs: save_path.parent.mkdir(parents=True) else: LOG.warning(f'Save storage location does not exist. Consider create_dirs=True. Not saving figure.') return DEFAULT_SAVE_FIG_KWARGS.update(kwargs) fig.savefig(save_path, **DEFAULT_SAVE_FIG_KWARGS) LOG.debug(f'Saved figure at {save_path} with settings: {DEFAULT_SAVE_FIG_KWARGS}')
def show_or_export(fig: plt.Figure, output_path=None): """ Show plot or export it to a file. :param fig: a plot figure :param output_path: Path or None. if None, show plot. otherwise, save to path. """ if output_path is None: plt.show() return fig.savefig(output_path)
def get_img_from_fig(fig: plt.Figure, dpi: int = 180) -> np.ndarray: buf = io.BytesIO() fig.savefig(buf, format="png", dpi=dpi) plt.close(fig) buf.seek(0) img_arr = np.frombuffer(buf.getvalue(), dtype=np.uint8) buf.close() img = cv2.imdecode(img_arr, 1) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) return img
def save_fig(path: str, fig: plt.Figure, fmt: str = "pdf", dpi: int = 300, **kwargs): path = f"{path}.{fmt}" root_dir = os.path.dirname(path) os.makedirs(root_dir, exist_ok=True) logging.info(f"Saving figure to {path}") kwargs.setdefault("transparent", True) with open(path, "wb") as f: fig.savefig(f, format=fmt, dpi=dpi, **kwargs)
def log_image(self, name: str, figure: Figure): if not self.__is_running: raise ValueError(""" It's necessary start the experiment using with statement Example: experiment = Experiment( ... ) with experiment.run(): experiment.log_image( ... ) """) figure.savefig(os.path.join(self.out_dir, f"{name}.jpg"))
def save_and_close(fig: plt.Figure, name: str) -> None: """save a figure and close it Parameters ---------- fig : :obj:`matplotlib.figure.Figure` the matplotlib figure to save name : str the filename to give the saved figure """ log.info("saving %s" % name) fig.savefig(name) plt.close(fig)
def save_figure(fig: plt.Figure, destination: str, extra_artist: mpl.artist.Artist=None): name = Path(destination) if extra_artist is not None: extra_args = {"extra_artists": (extra_artist, ), "bbox_inches": "tight"} else: extra_args = {} for extension in [".pdf", ".svgz", ".png"]: if extension != "png": fig.savefig(str(name.with_suffix(extension)), **extra_args) else: fig.savefig(str(name.with_suffix(extension)), dpi=300, **extra_args)
def saveplot(self,filename,**kwargs): dpi = kwargs.pop('dpi',72) format = kwargs.pop('format','png') backend_canvas = kwargs.pop('backend_canvas',FigureCanvasAgg) ins_orig = mpl.rcParams['svg.image_noscale'] mpl.rcParams['svg.image_noscale'] = True fig = Figure() canvas = backend_canvas(fig) ax = fig.add_subplot(111) self.plot(axes=ax,**kwargs) fig.savefig(filename,dpi=dpi,format=format) # Put svg image config back to original #mpl.rcParams['svg.embed_char_paths'] = ecp_orig mpl.rcParams['svg.image_noscale'] = ins_orig return filename