Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท Data and Figure Basics

Saving Seaborn Figures

Save seaborn plots to PNG, SVG or PDF with savefig, control DPI and transparency, avoid cropped labels with bbox_inches, and save FacetGrid figures.

A plot you cannot export is hard to share. Because seaborn figures are Matplotlib figures, saving works the same way: a savefig call with a file name, where the extension picks the format. A few options decide whether the result is sharp, correctly cropped and the right size for a slide, a web page or a print.

The basic call

Python
ax = sns.boxplot(data=tips, x="day", y="total_bill")
plt.savefig("bill_by_day.png", dpi=200, bbox_inches="tight")
  • dpi (dots per inch) sets the resolution of raster formats. The default is 100, so a 6.4 by 4.8 inch figure is 640 by 480 pixels. Use 150 to 300 for reports and print.
  • bbox_inches="tight" trims empty margins and includes labels or legends that sit outside the axes. Without it, a legend placed outside the plot often gets cut off.
  • Call savefig before plt.show(): after showing, a script's figure may be closed, and you would save an empty canvas.

Raster and vector formats

Format Type Best for
PNG Raster Web, slides, documents; supports transparency
JPG Raster Photos; avoid for charts (blurry edges)
SVG Vector Web pages, editing in design tools; scales without blur
PDF Vector Papers and print; embeds fonts
EPS Vector Older publishing workflows

Vector formats store lines and text as shapes, so they stay sharp at any size. A scatter plot with tens of thousands of points makes a large vector file; for those, PNG is smaller.

Figure-level functions

A FacetGrid, JointGrid or PairGrid has its own savefig method:

Python
g = sns.displot(data=df, x="bill", col="time")
g.savefig("bill_distribution.png", dpi=200)

g.savefig already crops tightly, so bbox_inches is not needed. You can also use g.figure.savefig(...). With plt.savefig after a figure-level call, the current figure is the grid's figure, so that works too.

Transparent backgrounds and themes

Python
plt.savefig("chart.png", transparent=True)

The figure and axes backgrounds become transparent. Be careful: the default seaborn theme uses a light grid and dark text, so a transparent PNG placed on a dark slide will be hard to read. Pick the theme to suit the destination (sns.set_theme(style="darkgrid"), or the dark options covered in the styling chapter).

Controlling size for a destination

Size in inches times DPI gives pixels. Decide the target first:

Destination Typical size (inches) DPI
Slide (16:9) 10 by 5.6 150
Report column 3.5 by 2.6 300
Blog post 8 by 4.5 150
Poster 20 by 12 150

Fonts do not shrink with the figure size, so do not draw a large figure and shrink it in a document: the text becomes tiny. Instead, draw at the final size and use sns.set_theme(context="paper") or "talk" to scale fonts (see the styling chapter).

Try it: save to memory

The browser sandbox on this site has no persistent disk, so we save into an in-memory buffer to see what savefig produces. The call is the same as for a real file; you simply pass a file name instead of the buffer.

Python savefig to PNG, SVG and PDF bytes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 300 dpi PNG is much bigger than the 100 dpi one (nine times the pixels), while SVG and PDF sizes do not depend on dpi. The first eight bytes \x89PNG\r\n\x1a\n are the PNG file signature.

A second example: a legend that would be cropped

Python Legend outside the axes and bbox_inches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The plain version keeps the original figure canvas, so the legend sticks out beyond it and is cut off; the tight version grows the image to include it (see the larger width in the printout).

Good habits

  • Save from code rather than screenshots, so the plot is reproducible.
  • Put the file name and DPI in one place (a function or a constant).
  • After saving in a loop, call plt.close() (or plt.close("all")) so you do not accumulate open figures and run out of memory.
  • Keep both a vector (SVG/PDF) and a PNG for anything you publish.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which savefig argument prevents an outside legend from being cut off?

    bbox_inches='tight' computes the bounding box of all artists, including legends and labels outside the axes.
  2. 2. What picks the file format in plt.savefig('chart.svg')?

    Matplotlib infers the format from the extension unless you pass format= explicitly.
  3. 3. You saved a FacetGrid g. Which is the idiomatic call?

    Grid objects have a savefig method that crops tightly.
  4. 4. For a figure you will place in a printed paper, which format keeps lines sharp at any size?

    PDF and SVG are vector formats and do not blur when scaled.

Summary

  • plt.savefig("name.ext", dpi=..., bbox_inches="tight") saves the current figure; the extension chooses the format.
  • Figure-level grids have their own g.savefig.
  • Use PNG for web and slides, SVG or PDF for print and scaling; avoid JPG for charts.
  • Save before plt.show(), and close figures in loops.
  • Decide size in inches and DPI for the destination, rather than resizing afterwards.

Next: Distributions: Visualizing One Variable