seaborn and interactive charts · lesson 4 of 10
Parts in this lesson
Asns.scatterplot(..., hue=, size=, style=)7.1: more columns, more aesthetics
Bsns.lineplot(data=df, x="t", y="a", hue="g")a line of means, with a band
Csns.regplot(...) / sns.lmplot(..., hue="g")a fitted straight line
Dalpha=0.05 · ax.hexbin(x, y) · sns.kdeplot(x=, y=)three cures for overplotting

Relationships

Last updated

Do longer bills come with deeper ones? Does a store sell more on some days of the week? Questions about how one variable moves with another are drawn as scatter plots and lines. In this lesson you'll add colour, size and shape to a scatter plot, draw lines with bands, fit straight lines and see one point the wrong way until you split it by group, rescue a chart of 50,000 points from being one blob, and stop a fitted line from running off past the data.

By the end of this lesson you can

  • Show three or four variables in one scatter plot with hue, size and style
  • Draw a lineplot with a confidence band and say what the band means
  • Fit lines with regplot and lmplot, and check them group by group
  • Fix overplotting with transparency, hexbins or a 2-D density

Scatter plots with more columns

Run the cells: bill length against bill depth.

bills.ipynb
  • The first cell maps four columns at once: position for the two bill measurements, colour for species, dot size for body mass, and marker style for island. It shows how far a scatter plot can go, and also where it stops: by the fourth column it's hard to read. Two or three columns per chart is usually plenty.
  • sns.regplot draws the dots and fits a straight line, with a shaded band for how sure the fit is. For all penguins together the line slopes down, and the correlation is −0.24: longer bills seem to go with shallower ones.
  • sns.lmplot, the figure-level version, takes hue="species" and fits one line per species. Every line slopes up: within each species the correlation is 0.39 (Adelie), 0.65 (Chinstrap) and 0.64 (Gentoo).

Both can't be true of penguins, and they aren't in conflict: the overall trend is made by the species. Gentoos have long, shallow bills; Adelies short, deep ones. Put them together and the line runs from one group to the other. This is Simpson's paradox: a trend in the whole data that reverses inside every group. Whenever your data has groups, check the relationship inside each one before you believe the overall line.

Lines and their bands

sns.lineplot is for an x-axis with an order: days, weeks, sizes. When there are several y values at each x, it draws their mean, with a band. Run the cells: a typical Tindahan week.

week.ipynb
  • The weekdays are made a pd.Categorical with an order, so the line runs Monday to Sunday rather than alphabetically.
  • Each point is the mean of that store's sales on that weekday; the second cell shows each is a mean of 8 or 9 days.
  • The shaded band is a 95% confidence interval for that mean, from a bootstrap, as in 7.3. A wide band means the days vary a lot or there are few of them. errorbar="sd" would show the spread of the days instead; errorbar=None drops the band.

Every store's line rises to Saturday, the weekly peak from Stage 6, and Batangas' band is the widest because its days vary most.

Too many points

With a few hundred points a scatter plot works. With tens of thousands the dots pile up, and a spot with 1 dot looks just like a spot with 500. That's overplotting. Try four ways to draw 50,000 receipts:

Overplotting fix50,000 receipts: items bought against pesos paid
sns.scatterplot(data=receipts, x="items", y="pesos")

One solid blob. You can see the edges of the data, but not where most receipts are: a spot with 1 point and a spot with 500 look the same.

Here are the same fixes in the notebook. Run the cells.

receipts.ipynb
  • Transparency: alpha=0.05 makes each dot 95% see-through, and linewidth=0 drops the white rim seaborn puts round each dot. Simple, and every point is still there.
  • Hexagonal bins: matplotlib's ax.hexbin counts the points in each hexagon and colours it; bins="log" colours by the logarithm of the count, so cells with a handful still show next to cells with thousands.
  • 2-D density: sns.kdeplot with both x= and y= draws contour bands. It's slow on many points, so the cell uses a random sample of 2,000, which is plenty for the shape.

(A chart with 50,000 dots makes a very big SVG, so this page sends any chart over 1.5 MB of SVG as a picture (PNG) instead. On your computer, ax.scatter(..., rasterized=True) does the same inside an SVG or PDF.)

Wrong way: a line past the data

A fitted line describes the data it was fitted to. Past the data, it's a guess. Run the cells.

flippers.ipynb

truncate=False lets the line run across the whole x-axis, and the cell widens the axis to start at 0. The fit says each extra millimetre of flipper adds about 50 g, which is reasonable for flippers from 172 to 231 mm, where the penguins are. At 0 mm, the same line says a penguin weighs −5,781 g.

Nothing is wrong with the arithmetic. The line was simply never told about flippers shorter than 172 mm, and straight lines don't know where to stop. seaborn's default, truncate=True, draws the line only across the range of the data, as the second cell shows. The check: never read a fitted line, or a prediction, outside the range of the data it came from. Stage 8 returns to this with sales and temperatures.

Check what you learned

Across all customers, bigger baskets have lower prices per item. Inside each store, bigger baskets have higher prices per item. What’s the likely explanation?
What does the shaded band around a sns.lineplot line show, by default?
A scatter plot of 200,000 orders is one dark blob. Which fix keeps every point and shows where most are?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis