Data Visualization with Seaborn
Seaborn is a statistical visualization library built on matplotlib. It provides a higher-level interface for drawing attractive charts with less code, and it handles the details of color palettes, legend placement, and statistical aggregations automatically.
Installing and Importing
pip install seaborn
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
Seaborn works best with pandas DataFrames. Its functions accept data in long format and automatically handle legends, labels, and styling.
Setting Styles
Seaborn ships with several themes that make charts look publication-ready out of the box:
sns.set_theme(style="whitegrid")
Common styles: whitegrid (good for most charts), darkgrid, white, dark, ticks. Setting the theme once applies it to all subsequent matplotlib figures in the session.
Loading Sample Data
Seaborn includes several built-in datasets:
tips = sns.load_dataset("tips")
print(tips.head())
# total_bill tip sex smoker day time size
# 0 16.99 1.01 Female No Sun Dinner 2
# 1 10.34 1.66 Male No Sun Dinner 3
# 2 21.01 3.50 Male No Sun Dinner 3
The tips dataset is the most commonly used example for learning Seaborn.
Scatter Plots and relplot
Use relplot() for relational plots:
sns.set_theme(style="whitegrid")
sns.relplot(data=tips, x="total_bill", y="tip", hue="smoker", style="time")
plt.show()
hue colors points by a column, style changes the marker shape. Both can be combined.
Line Plots
For continuous data with a dependent variable:
df = pd.DataFrame({
"day": range(1, 31),
"sales": np.random.randn(30).cumsum() + 100
})
sns.relplot(data=df, x="day", y="sales", kind="line", height=4, aspect=1.5)
plt.show()
kind="line" draws a line with a confidence band (95% CI by default). Disable it with ci=None.
Bar Charts with catplot
Categorical data:
sns.catplot(data=tips, x="day", y="total_bill", kind="bar", errorbar=None)
plt.show()
kind="bar" draws bars with error bars showing confidence intervals. Other kinds: strip, swarm, box, violin, boxen.
Box Plots
Show distributions across categories:
sns.catplot(data=tips, x="time", y="tip", kind="box", hue="smoker")
plt.show()
Box plots show median, quartiles, and outliers. hue adds a second categorical variable side by side.
Histograms and Distribution Plots
Show the distribution of a single variable:
sns.displot(data=tips, x="total_bill", bins=20, color="steelblue")
plt.show()
displot() with kind="hist" (default) draws a histogram. Add kde=True to overlay a kernel density estimate:
sns.displot(data=tips, x="total_bill", kde=True, bins=20)
plt.show()
Heatmaps
Visualize matrices or correlation tables:
flights = sns.load_dataset("flights")
flights_wide = flights.pivot(index="month", columns="year", values="passengers")
sns.heatmap(flights_wide, annot=True, fmt="d", cmap="YlGnBu")
plt.show()
Common parameters: annot=True shows values in cells, cmap sets the color palette, fmt formats the annotation.
Pair Plots
Visualize relationships across all numeric columns:
iris = sns.load_dataset("iris")
sns.pairplot(data=iris, hue="species", diag_kind="kde")
plt.show()
pairplot() draws scatter plots for each pair of variables and histograms (or KDE) on the diagonal. hue colors by a categorical column.
Color Palettes
Seaborn’s color_palette() lets you pick from built-in palettes:
sns.set_palette("husl")
current_palette = sns.color_palette("deep", n_colors=6)
sns.palplot(current_palette)
plt.show()
Built-in palettes: deep, muted, pastel, husl, Set2, colorblind. Pass a palette name to set_palette() to change the default for all plots, or pass it directly to a plot function via the palette argument.
Customizing with matplotlib
Seaborn returns a matplotlib axis object, so you can use standard matplotlib commands:
g = sns.relplot(data=tips, x="total_bill", y="tip", col="time", hue="smoker")
g.set_axis_labels("Total Bill ($)", "Tip ($)")
g.set_titles(col_template="{col_name} Time")
g.savefig("tips.png", dpi=150, bbox_inches="tight")
Use the Figure-level interface (relplot, catplot, displot) for multi-panel figures, or the Axes-level functions (scatterplot, boxplot, histplot) to embed charts in existing matplotlib layouts. Figure-level functions return a FacetGrid or PairGrid object; Axes-level functions return a matplotlib Axes object directly.
Axes-Level Alternatives
For single plots, Axes-level functions offer more granular control:
fig, ax = plt.subplots()
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="smoker", ax=ax)
sns.histplot(data=tips, x="total_bill", bins=15, kde=True, ax=ax)
ax.set_xlabel("Bill Amount ($)")
ax.set_ylabel("Frequency / Tip")
plt.show()
Mixing multiple plot types on the same Axes is useful for overlay plots. Pass ax=ax to embed the chart into any matplotlib subplot arrangement.
See Also
- /tutorials/scientific-python/matplotlib-basics/ — matplotlib foundation that seaborn builds on
- /tutorials/scientific-python/numpy-getting-started/ — data preparation for visualization
- /tutorials/scientific-python/pandas-getting-started/ — DataFrames are seaborn’s native data format
- /tutorials/scientific-python/pandas-data-cleaning/ — previous in the python-data-science series
- /tutorials/scientific-python/scipy-intro/ — next in the python-data-science series