save_fig¶
- pyhelpers.store.save_fig(path_to_file, dpi=None, verbose=False, conv_svg_to_emf=False, raise_error=False, **kwargs)[source]¶
Saves a figure object to a file in a supported format.
This function utilises the matplotlib.pyplot.savefig() function and optionally Inkscape for SVG to EMF conversion.
- Parameters:
path_to_file (str | os.PathLike) – The path where the figure file will be saved.
dpi (int | None) – Resolution in dots per inch; when
dpi=None
(default), it takes the value ofrcParams['savefig.dpi']
.verbose (bool | int) – Whether to print relevant information to the console; defaults to
False
.conv_svg_to_emf (bool) – Whether to convert a .svg file to a .emf file; defaults to
False
.raise_error (bool) – Whether to raise the provided exception; if
raise_error=False
(default), the error will be suppressed.kwargs – [Optional] Additional parameters for the function matplotlib.pyplot.savefig().
Examples:
>>> from pyhelpers.store import save_fig >>> from pyhelpers.dirs import cd >>> from pyhelpers.settings import mpl_preferences >>> mpl_preferences(backend='TkAgg') >>> import matplotlib.pyplot as plt >>> x, y = (1, 1), (2, 2) >>> fig = plt.figure() >>> ax = fig.add_subplot() >>> ax.plot([x[0], y[0]], [x[1], y[1]]) >>> fig.show()
The above exmaple is illustrated in Figure 13:
Figure 13 An example figure created for the function
save_fig()
.¶>>> img_dir = cd("tests", "images") >>> svg_file_pathname = cd(img_dir, "store-save_fig-demo.svg") >>> save_fig(svg_file_pathname, verbose=True) Saving "store-save_fig-demo.png" to ".\tests\images\" ... Done. >>> save_fig(svg_file_pathname, verbose=True, conv_svg_to_emf=True) Updating "store-save_fig-demo.svg" at ".\tests\images\" ... Done. Saving the .svg file as ".\tests\images\store-save_fig-demo.emf" ... Done. >>> plt.close()