matplotlib.pyplot.savefig#

matplotlib.pyplot.savefig(*args,**kwargs)[source]#

Save the current figure as an image or vector graphic to a file.

Call signature:

savefig(fname,*,transparent=None,dpi='figure',format=None,metadata=None,bbox_inches=None,pad_inches=0.1,facecolor='auto',edgecolor='auto',backend=None,**kwargs)

The available output formats depend on the backend being used.

Parameters:
fnamestr or path-like or binary file-like

A path, or a Python file-like object, orpossibly some backend-dependent object such asmatplotlib.backends.backend_pdf.PdfPages.

Ifformat is set, it determines the output format, and the fileis saved asfname. Note thatfname is used verbatim, and thereis no attempt to make the extension, if any, offname matchformat, and no extension is appended.

Ifformat is not set, then the format is inferred from theextension offname, if there is one. Ifformat is notset andfname has no extension, then the file is saved withrcParams["savefig.format"] (default:'png') and the appropriate extension is appended tofname.

Other Parameters:
transparentbool, default:rcParams["savefig.transparent"] (default:False)

IfTrue, the Axes patches will all be transparent; theFigure patch will also be transparent unlessfacecolorand/oredgecolor are specified via kwargs.

IfFalse has no effect and the color of the Axes andFigure patches are unchanged (unless the Figure patchis specified via thefacecolor and/oredgecolor keywordarguments in which case those colors are used).

The transparency of these patches will be restored to theiroriginal values upon exit of this function.

This is useful, for example, for displayinga plot on top of a colored background on a web page.

dpifloat or 'figure', default:rcParams["savefig.dpi"] (default:'figure')

The resolution in dots per inch. If 'figure', use the figure'sdpi value.

formatstr

The file format, e.g. 'png', 'pdf', 'svg', ... The behavior whenthis is unset is documented underfname.

metadatadict, optional

Key/value pairs to store in the image metadata. The supported keysand defaults depend on the image format and backend:

  • 'png' with Agg backend: See the parametermetadata ofprint_png.

  • 'pdf' with pdf backend: See the parametermetadata ofPdfPages.

  • 'svg' with svg backend: See the parametermetadata ofprint_svg.

  • 'eps' and 'ps' with PS backend: Only 'Creator' is supported.

Not supported for 'pgf', 'raw', and 'rgba' as those formats do not supportembedding metadata.Does not currently support 'jpg', 'tiff', or 'webp', but may includeembedding EXIF metadata in the future.

bbox_inchesstr orBbox, default:rcParams["savefig.bbox"] (default:None)

Bounding box in inches: only the given portion of the figure issaved. If 'tight', try to figure out the tight bbox of the figure.

pad_inchesfloat or 'layout', default:rcParams["savefig.pad_inches"] (default:0.1)

Amount of padding in inches around the figure when bbox_inches is'tight'. If 'layout' use the padding from the constrained orcompressed layout engine; ignored if one of those engines is not inuse.

facecolorcolor or 'auto', default:rcParams["savefig.facecolor"] (default:'auto')

The facecolor of the figure. If 'auto', use the current figurefacecolor.

edgecolorcolor or 'auto', default:rcParams["savefig.edgecolor"] (default:'auto')

The edgecolor of the figure. If 'auto', use the current figureedgecolor.

backendstr, optional

Use a non-default backend to render the file, e.g. to render apng file with the "cairo" backend rather than the default "agg",or a pdf file with the "pgf" backend rather than the default"pdf". Note that the default backend is normally sufficient. SeeThe builtin backends for a list of valid backends for eachfile format. Custom backends can be referenced as "module://...".

orientation{'landscape', 'portrait'}

Currently only supported by the postscript backend.

papertypestr

One of 'letter', 'legal', 'executive', 'ledger', 'a0' through'a10', 'b0' through 'b10'. Only supported for postscriptoutput.

bbox_extra_artistslist ofArtist, optional

A list of extra artists that will be considered when thetight bbox is calculated.

pil_kwargsdict, optional

Additional keyword arguments that are passed toPIL.Image.Image.save when saving the figure.

Notes

Note

This is thepyplot wrapper forFigure.savefig.

Examples usingmatplotlib.pyplot.savefig#

Print image to stdout

Print image to stdout

Rasterization for vector graphics

Rasterization for vector graphics

Apply SVG filter to a line

Apply SVG filter to a line

SVG filter pie

SVG filter pie

CanvasAgg demo

CanvasAgg demo

SVG Histogram

SVG Histogram

SVG Tooltip

SVG Tooltip