save_fig

pyhelpers.store.save_fig(path_to_file, dpi=None, verbose=False, conv_svg_to_emf=False, **kwargs)[source]

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

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

Parameters:
  • path_to_file (str | os.PathLike) – Path where a figure file is saved.

  • dpi (int | None) – Resolution in dots per inch; when dpi=None (default), it uses rcParams['savefig.dpi'].

  • verbose (bool | 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.

Examples:

>>> from pyhelpers.store 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]])
>>> plt.show()

The above exmaple is illustrated in Fig. 8:

../_images/store-save_fig-demo.svg

Fig. 8 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, 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()