课程 · 10 · 07 / 10
Seaborn: Statistical Data Visualization
Elevate your visualizations with seaborn. Create beautiful statistical graphics with minimal code: distributions, relationships, and categorical plots.
TIPLearning Objectives: After this lesson, you'll create beautiful statistical graphics with minimal code using seaborn—distributions, relationships, categorical comparisons, and styled presentations.
Why Seaborn?
Seaborn is built on matplotlib but provides a high-level interface for creating attractive statistical graphics. What takes 10 lines in matplotlib often takes 1 line in seaborn.
Setting Up Seaborn
Distribution Plots
Understanding data distributions is fundamental to data analysis.
Histograms and KDE
Box Plots and Violin Plots
Relationship Plots
Explore relationships between variables.
Scatter Plots with Regression
Pair Plots
Visualize relationships between all pairs of variables:
Heatmaps
Visualize matrices and correlations:
Categorical Plots
Compare categories effectively.
Bar Plots with Error Bars
Count Plots
Strip and Swarm Plots
Show individual data points:
Combining Plots
FacetGrid: Multiple Subplots
Create a grid of plots based on data subsets:
Complete Example: EDA Dashboard
Key Takeaways
✅ Distribution plots: histplot(), kdeplot(), boxplot(), violinplot()
✅ Relationship plots: scatterplot(), regplot(), pairplot(), heatmap()
✅ Categorical plots: barplot(), countplot(), stripplot(), swarmplot()
✅ FacetGrid: Create grids of plots by data subsets
✅ Styling: set_theme(), color palettes, context settings
✅ Integration: Works seamlessly with pandas DataFrames
Connections: Seaborn in Practice
🔗 Connection to Statistics
| Statistical Concept | Seaborn Plot |
|---|---|
| Distribution | histplot(), kdeplot() |
| Central tendency | barplot() (shows mean) |
| Spread | boxplot(), violinplot() |
| Correlation | heatmap(), pairplot() |
| Regression | regplot(), lmplot() |
🔗 When to Use Which Plot
| Question | Plot Type |
|---|---|
| What's the distribution? | histogram, KDE, box |
| Compare categories? | bar, count, box |
| Relationship between 2 vars? | scatter, regression |
| All pairwise relationships? | pairplot |
| Matrix of values? | heatmap |
Practice Exercises
Exercise 1: Complete Visualization
Next Steps
Now you're ready for Exploratory Data Analysis (EDA)—a systematic approach to understanding data by combining all the techniques you've learned.
Ready to explore real datasets? EDA is next!
Further Reading
Visual Galleries
- Seaborn Example Gallery — every chart type with thumbnail + code. Bookmark.
- From Data to Viz — flowchart-driven guide: pick your data shape, get the right chart and the seaborn snippet.
- Python Graph Gallery — Seaborn — 200+ examples organized by chart type.
Official Docs
- Seaborn — Tutorial — official walkthrough.
- Seaborn — Figure-level vs Axes-level Functions — the most important conceptual distinction in modern seaborn.
- Seaborn — Color Palettes — colorblind-safe and perceptually-uniform palettes (
viridis,cividis,colorblind).
Tutorials
- Real Python — Python Plotting With Seaborn — the canonical primer.
- seaborn.objects — declarative interface — the new pipe-friendly API.
Modern Declarative Alternatives
- Altair — Vega-Lite-based grammar of graphics. Most principled "declarative viz" library in Python.
- plotnine —
ggplot2ported to Python. If you came from R, start here.
Books
- Book: Storytelling with Data — Cole Nussbaumer Knaflic. The why of visualization.
- Book: Fundamentals of Data Visualization — Claus Wilke (free online). Library-agnostic; required reading for anyone making serious charts.