Seaborn Introduction
If matplotlib is the engine, seaborn is the paint job. Seaborn is built on top of matplotlib and makes statistical visualizations beautiful with minimal effort. It works seamlessly with pandas DataFrames — which is exactly what you want.
Why Seaborn Over Matplotlib?
Matplotlib gives you control; seaborn gives you convenience. A scatter plot with a regression line that would take 20 lines in matplotlib takes 1 in seaborn. Plus, the default themes look way better.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
tips = sns.load_dataset("tips")
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="sex")
plt.title("Tips by Gender")
plt.show()
Notice how seaborn reads directly from a DataFrame and automatically creates a legend from the hue parameter. That's the kind of convenience that saves hours.
Distribution Plots
Understanding how your data is distributed is fundamental. Seaborn makes this easy with several distribution plot types.
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
sns.histplot(data=tips, x="total_bill", kde=True, ax=axes[0])
axes[0].set_title("Histogram with KDE")
sns.boxplot(data=tips, x="day", y="total_bill", ax=axes[1])
axes[1].set_title("Box Plot by Day")
plt.tight_layout()
plt.show()
histplot shows distribution shape. boxplot reveals median, quartiles, and outliers. Use them together for a complete picture.
Relationship Plots
Seaborn excels at showing relationships between variables. The pairplot creates a matrix of scatter plots for every combination of numeric columns.
import seaborn as sns
iris = sns.load_dataset("iris")
sns.pairplot(iris, hue="species", diag_kind="kde")
plt.show()
A single line creates an entire exploration dashboard. This is one of seaborn's killer features — instant multi-variable analysis.
Try it Yourself →Heatmaps
Heatmaps show correlations and matrices beautifully. They're essential for correlation analysis.
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
corr = iris.corr(numeric_only=True)
sns.heatmap(corr, annot=True, cmap="coolwarm", center=0)
plt.title("Correlation Heatmap")
plt.show()
The annot=True parameter adds numbers to each cell. The cmap parameter controls the color scheme — "coolwarm" is great for correlations since blue is negative, red is positive.
Key Takeaways
- Seaborn works natively with pandas DataFrames
- histplot and boxplot are essential for understanding distributions
- pairplot creates a matrix of scatter plots in one call
- heatmaps are the standard way to visualize correlation matrices