0
0
Matplotlibdata~5 mins

Combining Seaborn and Matplotlib

Choose your learning style9 modes available
Introduction

We combine Seaborn and Matplotlib to create beautiful and customized charts. Seaborn makes it easy to plot nice graphs, and Matplotlib lets us change details like labels and colors.

You want a quick, pretty chart but also need to add custom titles or labels.
You want to add extra lines or shapes on top of a Seaborn plot.
You want to change the size or style of a plot after creating it with Seaborn.
You want to save a Seaborn plot with specific formatting using Matplotlib.
You want to combine multiple plots in one figure using Matplotlib but use Seaborn for styling.
Syntax
Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

# Create a Seaborn plot
sns.scatterplot(data=data, x='x_column', y='y_column')

# Use Matplotlib to customize
plt.title('My Plot Title')
plt.xlabel('X Label')
plt.ylabel('Y Label')
plt.show()

Seaborn builds on Matplotlib, so you can use Matplotlib commands after creating a Seaborn plot.

Always call plt.show() at the end to display the plot.

Examples
This example shows a scatter plot of iris data with a custom title added using Matplotlib.
Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

# Load example data
iris = sns.load_dataset('iris')

# Seaborn scatter plot
sns.scatterplot(data=iris, x='sepal_length', y='sepal_width')

# Add title with Matplotlib
plt.title('Sepal Length vs Width')
plt.show()
Here, we rotate the x-axis labels to make them easier to read using Matplotlib.
Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

# Load data
penguins = sns.load_dataset('penguins')

# Create boxplot with Seaborn
sns.boxplot(data=penguins, x='species', y='body_mass_g')

# Rotate x-axis labels with Matplotlib
plt.xticks(rotation=45)
plt.show()
This example adds a grid to a Seaborn line plot using Matplotlib.
Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

# Load data
fmri = sns.load_dataset('fmri')

# Line plot with Seaborn
sns.lineplot(data=fmri, x='timepoint', y='signal', hue='event')

# Add grid with Matplotlib
plt.grid(True)
plt.show()
Sample Program

This program creates a scatter plot of tips vs total bill grouped by day using Seaborn. Then it adds a title, axis labels, legend title, and grid using Matplotlib.

Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

# Load example dataset
tips = sns.load_dataset('tips')

# Create a Seaborn scatter plot
sns.scatterplot(data=tips, x='total_bill', y='tip', hue='day')

# Customize with Matplotlib
plt.title('Tips vs Total Bill by Day')
plt.xlabel('Total Bill ($)')
plt.ylabel('Tip ($)')
plt.legend(title='Day of Week')
plt.grid(True)

# Show the plot
plt.show()
OutputSuccess
Important Notes

You can mix Seaborn and Matplotlib commands freely because Seaborn uses Matplotlib under the hood.

Always call plt.show() once after all customizations to display the final plot.

Use Matplotlib to add annotations, change figure size, or save plots with specific settings.

Summary

Seaborn makes beautiful plots easily.

Matplotlib lets you customize those plots in many ways.

Combining both gives you power and flexibility for your charts.