savefig#

savefig(filename, fig=None, dpi=None, comments=None, pipfreeze=False, relframe=0, folder=None, makedirs=True, die=True, verbose=True, **kwargs)[source]#

Save a figure, including metadata

Wrapper for Matplotlib’s plt.savefig() function which automatically stores metadata in the figure. By default, it saves (git) information from the calling function. Additional comments can be added to the saved file as well. These can be retrieved via sc.loadmetadata().

Metadata can be stored and retrieved for PNG or SVG. Metadata can be stored for PDF, but cannot be automatically retrieved.

Parameters:
  • filename (str/Path) – name of the file to save to

  • fig (Figure) – the figure to save (if None, use current)

  • dpi (int) – resolution of the figure to save (default 200 or current default, whichever is higher)

  • comments (str) – additional metadata to save to the figure

  • pipfreeze (bool) – whether to store the contents of pip freeze in the metadata

  • relframe (int) – which calling file to try to store information from (default 0, the file calling sc.savefig())

  • folder (str/Path) – optional folder to save to (can also be provided as part of the filename)

  • makedirs (bool) – whether to create folders if they don’t already exist

  • die (bool) – whether to raise an exception if metadata can’t be saved

  • verbose (bool) – if die is False, print a warning if metadata can’t be saved

  • kwargs (dict) – passed to fig.save()

Examples:

plt.plot([1,3,7])

sc.savefig('example1.png')
print(sc.loadmetadata('example1.png'))

sc.savefig('example2.png', comments='My figure', freeze=True)
sc.pp(sc.loadmetadata('example2.png'))
New in version 1.3.3.
New in version 3.0.0: “freeze” renamed “pipfreeze”; “frame” replaced with “relframe”; replaced metadata with sc.metadata()