Course contentsShow
← Data Science
Lesson 1280 of 2,14532. Visualization with Matplotlib and SeabornFree lesson

Combining Matplotlib and Seaborn in Subplots

Integrate Seaborn plots into Matplotlib subplot layouts by passing axes objects to Seaborn functions.

Combining Matplotlib and Seaborn in Subplots

What you'll learn: How to mix Matplotlib and Seaborn plots within the same figure by passing axes objects to Seaborn functions.

The Core Idea

Matplotlib and Seaborn aren't rivals—they're partners. Seaborn is actually built on top of Matplotlib, which means you can seamlessly combine them. When you create subplots with plt.subplots(), each subplot returns an axes object. Seaborn functions accept an ax parameter that lets you draw Seaborn plots directly into those specific axes.

Think of it like a restaurant kitchen: Matplotlib provides the stations (subplots), and Seaborn is a specialized chef who can work at any station you assign them to.

How It Works in Practice

Instead of letting Seaborn create its own figure, you pass it a specific axes object:

import matplotlib.pyplot as plt
import seaborn as sns

# Create a 1x2 subplot layout
fig, axes = plt.subplots(1, 2, figsize=(12, 4))

# Use Matplotlib on the left subplot
axes[0].hist(data['column_a'], bins=20, color='skyblue')
axes[0].set_title('Matplotlib Histogram')

# Use Seaborn on the right subplot by passing ax parameter
sns.boxplot(x='category', y='value', data=data, ax=axes[1])
axes[1].set_title('Seaborn Box Plot')

plt.tight_layout()
plt.show()

The magic happens with ax=axes[1]—this tells Seaborn exactly where to draw. You can mix and match freely: scatter plots next to violin plots, heatmaps next to line charts, whatever your analysis needs.

Why This Matters

This technique gives you the best of both worlds: Matplotlib's fine-grained control over layout and Seaborn's beautiful statistical visualizations, all in one organized figure.

Key Takeaway: Pass Matplotlib axes objects to Seaborn functions using the ax parameter to create complex multi-plot figures that combine both libraries' strengths.