save_figure

pyhelpers.store.save_figure(data, path_to_file, verbose=False, conv_svg_to_emf=False, **kwargs)[source]

Save a figure object to a file of a supported file format. (An alternative to save_fig().)

Parameters:
  • data (matplotlib.Figure | seaborn.FacetGrid) – A figure object.

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

  • 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_figure
>>> from pyhelpers.dirs import cd
>>> from pyhelpers.settings import mpl_preferences
>>> import matplotlib.pyplot as plt
>>> import numpy as np

>>> mpl_preferences()

>>> x = np.linspace(-5, 5)
>>> y = 1 / (1 + np.exp(-x))

>>> fig = plt.figure()
>>> plt.plot(x, y)
>>> plt.show()

The above exmaple is illustrated in Fig. 9:

../_images/store-save_figure-demo.svg

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

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

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

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

>>> plt.close()