Save a Seaborn Plot to a File in Python: PNG, PDF, JPG, DPI, and More
Save a Seaborn Plot to a File in Python, Creating a beautiful chart with Seaborn is only half the job. In many real-world projects, you also need to save that visualization as a file so it can be added to a report, presentation, website, dashboard, research paper, or business document.
Fortunately, saving a Seaborn chart is straightforward because Seaborn works on top of Matplotlib. Once you create the plot, you can use Matplotlib’s savefig() method to export it in formats such as PNG, JPG, SVG, and PDF.
In this guide, you’ll learn how to save Seaborn plots, remove unnecessary whitespace, control image resolution, set custom dimensions, and export publication-quality charts.
The Basic Syntax for Saving a Seaborn Plot
A simple Seaborn visualization can be saved using the following pattern:
import seaborn as sns
plot = sns.lineplot(x=x, y=y)
figure = plot.get_figure()
figure.savefig("my_plot.png")
The important part is savefig().
The filename extension determines the output format. For example:
figure.savefig("my_plot.png")
figure.savefig("my_plot.jpg")
figure.savefig("my_plot.pdf")
figure.savefig("my_plot.svg")
For most web and general-purpose applications, PNG is a good choice. For documents that require scalable graphics, PDF or SVG can be preferable.
Example 1: Save a Seaborn Line Plot as a PNG
Let’s start with a simple line chart.
import seaborn as sns
sns.set_style("darkgrid")
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
plot = sns.lineplot(x=x, y=y)
figure = plot.get_figure()
figure.savefig("my_lineplot.png")
print("Plot saved successfully.")
After running the code, Python creates:
my_lineplot.png
in the current working directory.
You can check the location where Python is saving the file with:
import os
print(os.getcwd())
This is particularly useful when you’re working in Jupyter Notebook, VS Code, Google Colab, or another development environment and aren’t sure where the exported image went.
Example 2: Save the Plot Without Extra Whitespace
Have you ever exported a chart and noticed a large amount of empty space surrounding it?
The bbox_inches="tight" option can help.
import seaborn as sns
sns.set_style("darkgrid")
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
plot = sns.lineplot(x=x, y=y)
figure = plot.get_figure()
figure.savefig(
"my_lineplot_tight.png",
bbox_inches="tight"
)
The bbox_inches="tight" setting tells Matplotlib to calculate a tighter bounding box around the figure.
This is especially useful when you’re inserting charts into:
- PowerPoint presentations
- Word documents
- Research papers
- Business reports
- Websites
- Blog articles
Example 3: Increase the Image Resolution with DPI
The quality of an exported raster image depends heavily on its resolution.
You can control this using the dpi parameter.
figure.savefig(
"my_lineplot_high_resolution.png",
dpi=300
)
For example:
import seaborn as sns
sns.set_style("darkgrid")
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
plot = sns.lineplot(x=x, y=y)
figure = plot.get_figure()
figure.savefig(
"my_lineplot.png",
dpi=300,
bbox_inches="tight"
)
A higher DPI generally produces a sharper raster image, although it also increases the file size.
A practical rule of thumb is:
| Use case | Typical DPI |
|---|---|
| Quick preview | 72–100 |
| Website/blog | 100–150 |
| Reports | 150–200 |
| High-quality printing | 300 |
| Some publication workflows | 300–600 |
The exact requirement depends on where the image will be used.
Example 4: Save a Seaborn Plot with a Custom Size
DPI controls resolution, but it does not directly define the physical dimensions of the figure.
For that, use Matplotlib’s figsize.
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("darkgrid")
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
figure, axis = plt.subplots(figsize=(10, 6))
sns.lineplot(x=x, y=y, ax=axis)
figure.savefig(
"large_lineplot.png",
dpi=300,
bbox_inches="tight"
)
plt.close(figure)
The figsize=(10, 6) argument specifies the figure size in inches.
Therefore, this creates a figure that is 10 inches wide and 6 inches tall.
Combining figsize with dpi gives you much more control over the final image.
Example 5: Save a Seaborn Plot as a PDF
PNG isn’t always the best format.
If you’re creating a chart for a report, presentation, or publication, you may want a vector-based format such as PDF.
import matplotlib.pyplot as plt
import seaborn as sns
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
figure, axis = plt.subplots(figsize=(10, 6))
sns.lineplot(x=x, y=y, ax=axis)
figure.savefig(
"my_lineplot.pdf",
bbox_inches="tight"
)
plt.close(figure)
One advantage of vector formats such as PDF is that the graphic can be scaled without the same pixelation problems associated with raster images.
Example 6: Save a Seaborn Plot as JPG
You can also export the chart as a JPEG image.
import matplotlib.pyplot as plt
import seaborn as sns
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
figure, axis = plt.subplots(figsize=(10, 6))
sns.lineplot(x=x, y=y, ax=axis)
figure.savefig(
"my_lineplot.jpg",
dpi=300,
bbox_inches="tight"
)
plt.close(figure)
JPEG can be useful when file size matters, although PNG is often preferable for charts because text and sharp edges can be affected by JPEG compression.
Example 7: Save a Plot with a Transparent Background
If you’re placing a chart on a colored webpage, slide, or design, you may not want a white background.
Matplotlib allows you to create a transparent PNG:
import matplotlib.pyplot as plt
import seaborn as sns
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
figure, axis = plt.subplots(figsize=(10, 6))
sns.lineplot(x=x, y=y, ax=axis)
figure.savefig(
"transparent_plot.png",
dpi=300,
bbox_inches="tight",
transparent=True
)
plt.close(figure)
The transparent=True option makes the figure background transparent.
This can be particularly useful for presentations, websites, dashboards, and graphic design workflows.
Example 8: Save a Seaborn Scatter Plot
The same approach works with other Seaborn chart types.
For example, here’s a scatter plot:
import matplotlib.pyplot as plt
import seaborn as sns
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
figure, axis = plt.subplots(figsize=(8, 5))
sns.scatterplot(x=x, y=y, ax=axis)
axis.set_title("Sales Performance")
axis.set_xlabel("Month")
axis.set_ylabel("Sales")
figure.savefig(
"sales_scatterplot.png",
dpi=300,
bbox_inches="tight"
)
plt.close(figure)
The important idea is that savefig() isn’t limited to line charts. You can use it with practically any Seaborn visualization.
Example 9: Save Multiple Seaborn Charts
Suppose you’re generating several charts automatically from a dataset. You can save each figure separately.
import matplotlib.pyplot as plt
import seaborn as sns
data = {
"month": [1, 2, 3, 4, 5, 6],
"sales": [120, 150, 180, 160, 210, 250]
}
figure, axis = plt.subplots(figsize=(10, 6))
sns.lineplot(
x=data["month"],
y=data["sales"],
marker="o",
ax=axis
)
axis.set_title("Monthly Sales")
axis.set_xlabel("Month")
axis.set_ylabel("Sales")
figure.savefig(
"monthly_sales.png",
dpi=300,
bbox_inches="tight"
)
plt.close(figure)
Calling plt.close() after saving is a good habit when you’re generating many figures programmatically because it releases the figure from memory.
Example 10: Create a Reusable Function to Save Seaborn Charts
If you frequently export charts, you can create a small helper function.
import matplotlib.pyplot as plt
import seaborn as sns
def save_seaborn_plot(
filename,
width=10,
height=6,
dpi=300
):
figure = plt.gcf()
figure.set_size_inches(width, height)
figure.savefig(
filename,
dpi=dpi,
bbox_inches="tight"
)
plt.close(figure)
sns.set_style("darkgrid")
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
sns.lineplot(x=x, y=y)
save_seaborn_plot(
"final_chart.png",
width=10,
height=6,
dpi=300
)
This approach is useful in data-analysis pipelines where dozens or hundreds of charts need to be generated consistently.
Which File Format Should You Use?
Choosing the right format depends on how you intend to use the visualization.
| Format | Best for | Main advantage |
|---|---|---|
| PNG | Websites, reports, dashboards | Excellent quality for charts |
| JPG | Photos and smaller raster files | Smaller file size |
| Reports and publications | Vector-based output | |
| SVG | Websites and scalable graphics | Scales without pixelation |
For a typical data-science blog post, PNG at around 150–300 DPI is usually a practical choice.
For professional documents or publication workflows, check the requirements of the destination before selecting a format and resolution.
A Common Mistake: Saving the Wrong Figure
When several charts are being created, it is easy to accidentally save the wrong figure if you rely on the current global figure.
A safer approach is to explicitly create a figure and axis:
figure, axis = plt.subplots(figsize=(10, 6))
sns.lineplot(
x=x,
y=y,
ax=axis
)
figure.savefig("my_plot.png", dpi=300)
This makes it clear exactly which figure is being saved.
Another Common Mistake: Calling plt.show() First
In some environments, users save the figure after displaying it:
plt.show()
figure.savefig("plot.png")
Depending on the environment and plotting workflow, this can sometimes lead to unexpected results.
A safer pattern is:
figure.savefig("plot.png", dpi=300)
plt.show()
Or, when the chart only needs to be saved:
figure.savefig("plot.png", dpi=300)
plt.close(figure)
How to Save a Seaborn Plot to a Specific Folder
You don’t have to save the image in the current working directory.
You can specify a complete path:
figure.savefig(
"charts/monthly_sales.png",
dpi=300,
bbox_inches="tight"
)
For production scripts, pathlib provides a clean way to manage output folders:
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns
output_dir = Path("charts")
output_dir.mkdir(parents=True, exist_ok=True)
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
figure, axis = plt.subplots(figsize=(10, 6))
sns.lineplot(x=x, y=y, ax=axis)
output_file = output_dir / "sales_trend.png"
figure.savefig(
output_file,
dpi=300,
bbox_inches="tight"
)
plt.close(figure)
print(f"Saved to: {output_file}")
This approach automatically creates the charts directory if it doesn’t already exist.
The Best General-Purpose Pattern
For most projects, the following pattern is a reliable starting point:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme(style="darkgrid")
x = [1, 2, 3, 4, 5, 6]
y = [8, 13, 14, 11, 16, 22]
figure, axis = plt.subplots(figsize=(10, 6))
sns.lineplot(
x=x,
y=y,
marker="o",
ax=axis
)
axis.set_title("Sales Trend")
axis.set_xlabel("Month")
axis.set_ylabel("Sales")
figure.savefig(
"sales_trend.png",
dpi=300,
bbox_inches="tight"
)
plt.close(figure)
This gives you control over the figure size, resolution, layout, labels, and output file while avoiding unnecessary global plotting state.
Conclusion
Saving a Seaborn visualization is simple once you understand the relationship between Seaborn, Matplotlib figures, and savefig().
For a quick export, this is usually enough:
plot = sns.lineplot(x=x, y=y)
plot.get_figure().savefig("plot.png")
For higher-quality output, consider:
figure.savefig(
"plot.png",
dpi=300,
bbox_inches="tight"
)
And when you need precise control over the chart, explicitly create the figure with plt.subplots() and pass the axis to Seaborn.
Whether you’re building an automated analytics pipeline, preparing a business report, publishing a data-science article, or creating graphics for a presentation, these techniques give you a reliable way to turn your Seaborn visualizations into reusable files.