Seaborn
Data Visualization
Plotting
Python
Save Plot

How to save a Seaborn plot into a file

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Seaborn sits on top of Matplotlib, so saving a Seaborn plot to a file is usually done through Matplotlib’s figure-saving APIs. The main details that matter are which figure you save, whether you save before show, and what output settings such as DPI and bounding box you choose.

Save the current figure with plt.savefig

For simple plotting scripts, the direct pattern is:

python
1import seaborn as sns
2import matplotlib.pyplot as plt
3
4tips = sns.load_dataset("tips")
5sns.scatterplot(data=tips, x="total_bill", y="tip")
6
7plt.savefig("scatter.png", dpi=300, bbox_inches="tight")

This works because Seaborn draws onto the active Matplotlib figure, and plt.savefig writes that figure to disk.

dpi=300 is a common choice for high-quality raster output, and bbox_inches="tight" trims extra whitespace.

Save before calling plt.show

In scripts, the safest habit is to save the figure before plt.show().

python
1import seaborn as sns
2import matplotlib.pyplot as plt
3
4tips = sns.load_dataset("tips")
5sns.histplot(data=tips, x="total_bill")
6
7plt.savefig("histogram.png", dpi=200)
8plt.show()

Depending on the environment and backend, show() can finalize or clear figure state. Saving first avoids that ambiguity.

Save an explicit figure object

In larger programs, it is cleaner to save the figure object directly instead of relying on global state.

python
1import seaborn as sns
2import matplotlib.pyplot as plt
3
4tips = sns.load_dataset("tips")
5fig, ax = plt.subplots(figsize=(8, 5))
6sns.boxplot(data=tips, x="day", y="total_bill", ax=ax)
7
8fig.savefig("boxplot.pdf", bbox_inches="tight")

This makes it obvious which figure is being saved and works especially well when multiple figures exist at once.

Figure-level Seaborn plots need the returned object

Some Seaborn functions such as relplot, catplot, and displot create figure-level objects. In those cases, save through the figure attached to the returned object.

python
1import seaborn as sns
2
3tips = sns.load_dataset("tips")
4g = sns.relplot(data=tips, x="total_bill", y="tip", hue="day")
5
6g.figure.savefig("relplot.png", dpi=300, bbox_inches="tight")

This is important because figure-level Seaborn functions do not behave exactly like simple axes-level plotting calls.

Choose the right file format

Common output formats include:

  • PNG for general image use
  • PDF for vector output in reports
  • SVG for scalable web or design workflows

Examples:

python
plt.savefig("chart.png")
plt.savefig("chart.pdf")
plt.savefig("chart.svg")

If the image will be resized later, vector formats such as PDF or SVG often preserve quality better than raster formats.

Use transparency and cleanup when needed

If the output should blend with a slide deck or a web background, transparency can help:

python
plt.savefig("chart.png", transparent=True, bbox_inches="tight")

And if your script generates many plots, close the figures after saving so memory usage does not keep growing:

python
1import matplotlib.pyplot as plt
2
3fig, ax = plt.subplots()
4sns.histplot(data=tips, x="tip", ax=ax)
5fig.savefig("tip_hist.png")
6plt.close(fig)

This matters much more in loops or report-generation scripts than in casual interactive work.

Common Pitfalls

The biggest mistake is saving after the figure has already been cleared, closed, or shown in an environment where show() finalizes the display state. Saving first is safer.

Another issue is assuming every Seaborn function returns the same kind of object. Axes-level and figure-level functions expose different handles, so the correct save call can differ.

Developers also forget to tune output options. The file saves successfully, but the result looks blurry or has awkward whitespace because DPI and bounding-box settings were left at defaults.

Finally, when generating many figures, forgetting to close them can waste memory and produce warnings about too many open plots.

Summary

  • Save Seaborn plots through Matplotlib’s savefig APIs.
  • 'plt.savefig(...) is enough for many simple scripts.'
  • Save figure-level Seaborn plots through the returned figure object.
  • Save before plt.show() when you want predictable script behavior.
  • Choose file format, DPI, transparency, and figure cleanup based on the real output use case.

Course illustration
Course illustration

All Rights Reserved.