1Matplotlib against Seaborn
| Matplotlib | Seaborn | |
|---|---|---|
| Level | Low, very flexible | High level interface |
| Data input | Arrays and lists, subset manually | pandas DataFrame passed as data= |
| Syntax | Verbose | Concise |
| Statistics | Manual, you compute the mean yourself | Built in, means and confidence intervals automatic |
| Aesthetics | Basic defaults | Pleasing defaults |
| Purpose | Full customisation | Statistical visualisation |
The statistics difference
sns.barplot does that you did not ask for
Combining the two
sns.boxplot(data=df, x='course', y='exam_score') # Seaborn draws
plt.title('Exam Scores by Course') # Matplotlib customises
plt.ylabel('Final Score')
plt.axhline(df['exam_score'].mean(), color='r', linestyle='--')
plt.show()
The standard working pattern: Seaborn for the main visual, Matplotlib for titles, reference lines and annotations.
A horizontal average line added this way turns a distribution chart into a comparison against a benchmark, which is Chapter 5's "context makes a number meaningful".
Matplotlib remains the core library for exploration. Seaborn is what you reach for when the chart goes into a report or a dashboard.
The architectural idea: figure-level and axes-level
This is the concept that makes Seaborn make sense, and the one most people miss.
relplot(kind='line') gives you lineplot with faceting on top.Semantic mappings: hue, style, size
The core Seaborn idea. Instead of drawing several series manually, you name a column and Seaborn maps it to a visual channel.
hue and style for the same variable and the distinction becomes much easier to read, because colour alone makes the brain process an extra layer while different shapes make the grouping immediate through the similarity principle. Map them to different variables and one plot carries four dimensions: x, y, colour and shape.hue and style are vivid on screen and much weaker in black and white. If the chart will be printed, do not rely on colour alone. This is Chapter 3's accessibility rule. sizes=(15, 200) sets a custom size range and Seaborn scales the data into it, and without it raw values may produce unreadable markers.
Line plots and statistical aggregation
lineplot aggregates them into a mean automatically. The shaded band is the confidence interval, and its width tells you how dispersed the underlying values are at that point. A wide band means dispersed data, a narrow one means tightly packed. That turns a plain line into a statistical statement: you see both the trend and how much to trust it.Faceting: small multiples, automatically
col= and row= build the grid for you
Categorical plots
All reachable through catplot(kind=...) or as individual axes-level functions.
Distributions
| Function | Purpose |
|---|---|
histplot | Univariate distribution as bars |
kdeplot | Smoothed density estimate |
ecdfplot | Cumulative distribution |
rugplot | Ticks marking individual observations |
jointplot | Bivariate scatter with both marginal distributions on the axes |
pairplot | Every variable against every other |
jointplot and pairplot, the Chapter 1 and Chapter 2 charts in one commandjointplot is the joint plot from Chapter 1: a scatter with the distribution of x and y drawn along the axes, so you can check whether a relationship holds across the whole range or only part of it. pairplot is the scatter plot matrix from Chapter 2 in one command, and it is the fastest exploratory sweep over a new dataset. displot is the figure-level wrapper that adds faceting to these.Regression plots
Regression estimates how much an independent variable influences a dependent one. The course is not teaching the statistics, it is teaching how to present the result so an audience accepts it.
regplot | lmplot | |
|---|---|---|
| Level | Axes | Figure |
| Data format | More flexible | x and y must be column-name strings |
| Supports hue, col, row | No | Yes |
lmplot when you want separate regressions per group. The regression line does not pass through every point, it captures the overall relationship among them. In the tips data, total bill is the independent variable and tip the dependent one, because the bill influences the tip and not the reverse. Getting this the right way round is the analyst's job, not the software's.| Argument | Effect |
|---|---|
ci=None | Removes the confidence band around the line |
scatter_kws={'s': 80} | Sets the size of the scattered points |
order=2 | Fits a polynomial of that degree instead of a straight line |
Anscombe's quartet, revisited
order=2 fits the curve properly. That is the argument for visualising every regression before reporting it, and it is the same conclusion as Chapter 8's polynomial-against-linear forecast.Key points
- Six differences between Matplotlib and Seaborn.
- Why
sns.barplotneeds one line where Matplotlib needs agroupby. - The three things
barplotdoes automatically, including ordering. - The working pattern of Seaborn for the visual and Matplotlib for the finishing.
- Figure-level against axes-level functions, with examples of each.
- Why only figure-level functions can facet.
hue,styleandsize, and why combininghueandstylehelps.- Why colour alone fails in print.
- How
lineplotaggregates, and what the shaded band means. - What
estimator=Nonedoes. col=androw=faceting, and its link to small multiples.- Why
jitterandswarmplotexist. - The categorical plot family, and which shows summary against every point.
jointplotandpairplot, and their equivalents from Weeks 1 and 2.regplotagainstlmplot, and when the difference matters.- What
ci=Noneandorder=2do. - The Anscombe regression demonstration, and why every model should be plotted.