How to save a plot in Seaborn with Python (Matplotlib)?

To save a plot in Seaborn, we can use the savefig() method from Matplotlib. Since Seaborn is built on top of Matplotlib, we can save any Seaborn plot using this approach.

Basic Syntax

import matplotlib.pyplot as plt

# Create your Seaborn plot
# Then save it
plt.savefig('filename.png')

Example: Saving a Pairplot

Let's create a pairplot and save it to a file ?

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Set figure parameters
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create sample data
df = pd.DataFrame(np.random.random((5, 5)), columns=["a", "b", "c", "d", "e"])
print("Sample DataFrame:")
print(df.head())

# Create pairplot
sns_pp = sns.pairplot(df)
sns_pp.savefig("seaborn_pairplot.png")

print("Plot saved as 'seaborn_pairplot.png'")
Sample DataFrame:
          a         b         c         d         e
0  0.374540  0.950714  0.731994  0.598658  0.156019
1  0.155995  0.058084  0.866176  0.601115  0.708073
2  0.020584  0.969910  0.832443  0.212339  0.181825
3  0.183405  0.304242  0.524756  0.431945  0.291229
4  0.611853  0.139494  0.292145  0.366362  0.456070
Plot saved as 'seaborn_pairplot.png'

Using plt.savefig() Method

For most Seaborn plots, you can also use matplotlib's savefig() directly ?

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Create sample data
data = pd.DataFrame({
    'x': np.random.randn(100),
    'y': np.random.randn(100)
})

# Create scatter plot
plt.figure(figsize=(8, 6))
sns.scatterplot(data=data, x='x', y='y')
plt.title('Seaborn Scatter Plot')

# Save the plot
plt.savefig('scatter_plot.png', dpi=300, bbox_inches='tight')
print("Scatter plot saved with high resolution")

plt.show()
Scatter plot saved with high resolution

Common File Formats and Parameters

Parameter Description Example
filename File path and format 'plot.png', 'plot.pdf', 'plot.svg'
dpi Resolution (dots per inch) 300 for high quality
bbox_inches Bounding box 'tight' removes extra whitespace
facecolor Background color 'white', 'transparent'

Example with Multiple Parameters

import seaborn as sns
import matplotlib.pyplot as plt

# Load built-in dataset
tips = sns.load_dataset('tips')

# Create heatmap
plt.figure(figsize=(8, 6))
correlation_matrix = tips.select_dtypes(include=['float64', 'int64']).corr()
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', center=0)
plt.title('Tips Dataset Correlation Heatmap')

# Save with multiple parameters
plt.savefig('tips_heatmap.png', 
           dpi=300, 
           bbox_inches='tight',
           facecolor='white',
           edgecolor='none')

print("Heatmap saved with custom parameters")
plt.show()
Heatmap saved with custom parameters

Conclusion

Use savefig() method to save Seaborn plots either through the plot object or plt.savefig(). Include parameters like dpi=300 and bbox_inches='tight' for high-quality output with minimal whitespace.

Updated on: 2026-03-25T22:43:45+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements