Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 38 ยท Distributions

Two Variables: Bivariate Distributions

Visualize two variables at once with seaborn histplot and kdeplot using x and y: 2D histograms, density contours, levels, thresholds, color bars and hue.

A scatter plot shows every point, which works for a hundred rows and fails for a hundred thousand: points pile on top of each other (overplotting) and the dense areas turn into a solid blob. Bivariate distributions show where points are concentrated instead. Pass both x= and y= to histplot or kdeplot and seaborn draws a 2D histogram or a set of density contours.

2D histograms

histplot with x and y divides the plane into rectangles and colors each by its count.

Python Overplotted scatter vs 2D histogram

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The scatter plot on the left is a dark mass in the corner. The 2D histogram shows exactly where the density is highest and how it fades along the diagonal. Useful arguments:

  • bins=30 or (30, 20) sets the number of bins per axis; binwidth=(1, 2) sets widths in data units.
  • cbar=True adds a color bar explaining the counts, and cbar_kws={"label": "Trips"} labels it.
  • pthresh=0.05 hides the sparsest bins (those below that share of the maximum), which removes noise at the edges.
  • cmap="mako" or another colormap changes the colors; sequential maps (light to dark) suit counts.
  • stat="density" or "probability" changes what the colors mean.

2D KDEs: density contours

kdeplot with x and y estimates a smooth density surface and draws it as contour lines, like a topographic map. Each line connects points of equal density.

  • fill=True fills the bands with color.
  • levels=8 sets how many contour lines; levels=[0.1, 0.3, 0.6, 0.9] asks for contours enclosing roughly those shares of the data.
  • thresh=0.05 drops the outermost area, so the plot does not extend into empty regions.
  • bw_adjust controls smoothness as in the 1D case.
Python Density contours, filled and by group

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With hue, each group gets contour lines in its own color; it is much clearer than two overlapping scatter clouds. Avoid fill=True with many groups, because the fills hide each other.

Which one should I use?

Need Use
Exact counts, cell-level reading histplot(x, y)
Large data, a quick overview histplot(x, y, bins=..., pthresh=...)
A smooth picture of shape and correlation kdeplot(x, y)
Comparing groups of points kdeplot(hue=...) with unfilled contours
Seeing points and density together Scatter plot with kdeplot on top (see next lesson)

A 2D histogram is unbiased by smoothing choices but depends on bin size; a KDE looks pretty but may suggest density in places with no data.

Overlaying points on contours

You can draw several plots on one axes. A common recipe puts a faint scatter under unfilled contours:

Python
ax = sns.scatterplot(data=df, x="x", y="y", s=10, alpha=0.3)
sns.kdeplot(data=df, x="x", y="y", levels=5, color="black", linewidths=1, ax=ax)

Use alpha on the scatter so the contours stay visible.

Large datasets and log scales

When one axis is skewed (income, population, file size), log_scale=(True, False) bins or smooths on a log scale for x only. Combine with bins carefully: bins are computed on the transformed axis.

Common mistakes

  • Forgetting that bins are rectangles in data units: a very different aspect ratio of the two axes gives long, thin bins.
  • Reading a KDE contour as "this many points": contour levels are density, not counts. levels=[0.1, 0.5, 0.9] refers to the share of probability mass inside, which is handier.
  • Using fill=True with hue and many groups.
  • Leaving thresh at 0 for data with outliers: tiny faint regions stretch the axes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What problem do bivariate histograms and KDEs solve compared with scatter plots?

    With many points, overplotting hides density. These plots show it directly.
  2. 2. How do you draw a 2D histogram in seaborn?

    histplot switches to a 2D histogram when you pass both x and y.
  3. 3. What does thresh=0.05 do in kdeplot(x, y)?

    thresh hides the lowest density levels, keeping the plot compact.
  4. 4. Which is the clearest way to compare two groups with 2D KDEs?

    Unfilled contours in different colors let you see both groups without hiding either.

Summary

  • Passing x and y to histplot gives a 2D histogram (a grid of counts); to kdeplot gives density contours.
  • Use cbar=True, pthresh, bins and cmap to tune histograms; levels, thresh, fill and bw_adjust for KDEs.
  • hue with unfilled contours compares groups cleanly.
  • Both address overplotting in large datasets.
  • Combine a faint scatter with contours to show points and density together.

For a deeper look at one pair of variables with marginal histograms, see Joint Plots with Marginal Distributions.

Next: Scatter Plots with scatterplot