save_fig, dpi=None, verbose=False, conv_svg_to_emf=False, **kwargs)

Save a figure object to a file of a supported file format.

This function relies on matplotlib.pyplot.savefig (and Inkscape).

  • path_to_fig_file (str or os.PathLike[str]) – path where a figure file is saved

  • dpi (int or None) – the resolution in dots per inch; if None (default), use rcParams['savefig.dpi']

  • verbose (bool or int) – whether to print relevant information in console, defaults to False

  • conv_svg_to_emf (bool) – whether to convert a .svg file to a .emf file, defaults to False

  • kwargs – [optional] parameters of matplotlib.pyplot.savefig


>>> from import save_fig
>>> from pyhelpers.dirs import cd
>>> from pyhelpers.settings import mpl_preferences
>>> import matplotlib.pyplot as plt

>>> mpl_preferences()

>>> x, y = (1, 1), (2, 2)

>>> plt.figure()
>>> plt.plot([x[0], y[0]], [x[1], y[1]])

The above exmaple is illustrated in Fig. 9:


Fig. 9 An example figure created for the function save_fig().

>>> img_dir = cd("tests\images")

>>> png_file_pathname = cd(img_dir, "store-save_fig-demo.png")
>>> save_fig(png_file_pathname, dpi=600, verbose=True)
Saving "store-save_fig-demo.png" to "tests\images\" ... Done.

>>> svg_file_pathname = cd(img_dir, "store-save_fig-demo.svg")
>>> save_fig(svg_file_pathname, dpi=600, verbose=True, conv_svg_to_emf=True)
Saving "store-save_fig-demo.svg" to "tests\images\" ... Done.
Saving the .svg file as "tests\images\store-save_fig-demo.emf" ... Done.

>>> plt.close()