Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 38 ยท Multi-plot Grids

Joint Plots with Marginal Distributions

Combine a scatter plot with marginal histograms or density curves using jointplot and JointGrid: kinds, hue, hexbins, regression and custom margins.

A scatter plot tells you how two variables relate, but it hides how each variable is distributed on its own. Where are the values piled up? Is one variable skewed? A joint plot fixes this by adding a small distribution plot along the top (for x) and the right side (for y), called the marginals. You see the relationship and both distributions in one picture.

Seaborn has two tools: jointplot() for quick results, and JointGrid when you want to choose each part yourself.

jointplot basics

Python Scatter with marginal histograms

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.

jointplot() returns a JointGrid. Its three axes are g.ax_joint (the centre), g.ax_marg_x (top) and g.ax_marg_y (right). Like all figure-level functions it takes height, and the figure is square, with ratio controlling the centre-to-margin size (default 5) and space the gap.

The kind argument

kind changes the central plot, and the marginals adapt:

kind Centre Best for
"scatter" Points Up to a few thousand points
"kde" Density contours Smooth shape of a blob
"hist" 2D histogram Large data, counts per cell
"hex" Hexagonal bins Large data; hexes avoid square-grid artefacts
"reg" Scatter plus regression line Quick trend check
"resid" Residuals of a linear fit Model checking
Python Four kinds side by side

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.

For 1,500 points, plain scatter would already be crowded. hex and hist show where the mass is, and kde shows the shape. Choose by purpose: exploring density (hex, kde) or exploring individual points (scatter).

Adding hue

hue splits the central plot and the marginals by category. In the margins, groups become overlaid density curves, which makes differences between groups obvious:

Python jointplot with hue and KDE margins

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.

common_norm=False normalises each group's density separately, so a small group is not squashed flat by a large one.

JointGrid: choose every part

JointGrid creates the three axes; you fill the centre with plot_joint() and the margins with plot_marginals(). Both accept seaborn functions. You can even use two different functions, such as a scatter below and a density in the margins, or call plot_joint twice to layer plots:

Python Layered scatter and KDE with rug margins

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 dashed lines mark the means, added through g.ax_joint, which is a normal Matplotlib Axes. Any Matplotlib method works on the three axes.

When to use a joint plot

  • Two continuous variables, one question. Use a joint plot to study the pair closely; use a pair plot when you have many pairs.
  • Skew and clumps. A marginal can reveal that a variable is lopsided or has two humps, which a bare scatter plot does not show well.
  • Not for many groups. With more than three or four hue levels the centre becomes unreadable. Use a FacetGrid instead.

For a plain Matplotlib version of the same idea, see GridSpec layouts.

Common mistakes

  • Using figsize: joint plots are square, controlled by height.
  • Using ax=: figure-level, so no.
  • Calling plt.title(): it titles only the last axes; use g.figure.suptitle().
  • Plot-specific keywords: marginal_kws goes to the margins, joint_kws to the centre, and extra **kwargs also go to the centre.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the marginal plots in a joint plot?

    They show how each variable is distributed on its own.
  2. 2. Which kind is a good choice for 100,000 points?

    Binned plots show density, whereas 100,000 scatter points become a solid blob.
  3. 3. How do you set a figure title on a jointplot?

    The grid is a Figure with three axes. suptitle titles the whole figure.
  4. 4. Which JointGrid method fills the top and right panels?

    plot_joint draws the centre and plot_marginals draws the marginal axes.

Summary

  • jointplot() shows two variables plus both marginal distributions.
  • kind is one of scatter, kde, hist, hex, reg, resid; hue splits by group.
  • JointGrid plus plot_joint and plot_marginals gives complete control and layering.
  • It is figure-level: use height, g.figure and g.ax_joint.

Next: Ridgeline Plots and Small Multiples of Distributions