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 in a supported format (with the figure object specified).

This function serves an alternative to the save_fig() function.

Parameters:
  • data (matplotlib.Figure | seaborn.FacetGrid) – The figure object to be saved.

  • path_to_file (str | os.PathLike) – The path where the figure file will be saved.

  • 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.

  • kwargs – [Optional] Additional parameters for the function matplotlib.pyplot.savefig().

Examples:

>>> from pyhelpers.store import save_figure
>>> from pyhelpers.dirs import cd
>>> import numpy as np
>>> from pyhelpers.settings import mpl_preferences
>>> mpl_preferences(backend='TkAgg')
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(-5, 5)
>>> y = 1 / (1 + np.exp(-x))
>>> fig = plt.figure()
>>> ax = fig.add_subplot()
>>> ax.plot(x, y)
>>> fig.show()

The above exmaple is illustrated in Figure 14:

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

Figure 14 An example figure created for the function save_figure().

>>> img_dir = cd("tests\images")
>>> svg_file_pathname = cd(img_dir, "store-save_figure-demo.svg")
>>> save_figure(fig, svg_file_pathname, verbose=True)
Saving "store-save_figure-demo.png" to "tests\images\" ... Done.
>>> # save_figure(fig, "docs/source/_images/store-save_figure-demo.svg", verbose=True)
>>> # save_figure(fig, "docs/source/_images/store-save_figure-demo.pdf", verbose=True)
>>> save_figure(fig, svg_file_pathname, verbose=True, conv_svg_to_emf=True)
Updating "store-save_figure-demo.svg" at "tests\images\" ... Done.
Saving the .svg file as "tests\images\store-save_figure-demo.emf" ... Done.
>>> plt.close()