Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 38 ยท Multi-plot Grids

FacetGrid: Small Multiples in Seaborn

Split a dataset into a grid of subplots with FacetGrid: rows, columns, hue, map and map_dataframe, shared axes, titles, legends and sizing.

One chart can only hold so much. When you want to compare the same relationship across several groups, the clearest tool is small multiples: a grid of small charts, identical in design, one per subset of the data. Because the axes match, your eye compares the panels instantly.

Seaborn's FacetGrid builds such grids. It is the engine behind relplot(), displot(), catplot() and lmplot(), which is why those functions accept col, row and hue. In this lesson you use FacetGrid directly, which gives you full control: you choose the plotting function, pass arguments to it and tweak the grid afterwards.

Figure-level versus axes-level

Seaborn has two kinds of plotting functions. Axes-level functions (scatterplot, histplot, boxplot) draw on one Axes and accept ax=. Figure-level functions (relplot, displot, catplot, lmplot, pairplot, jointplot) create their own figure and layout and return a grid object. FacetGrid is the general-purpose bridge: you give it data and a layout, then map any axes-level function onto every panel.

Building a FacetGrid

Create the grid FacetGrid(data, col=..., row=..., hue=...)
Map a plot g.map_dataframe(sns.scatterplot, x=..., y=...)
Polish Labels, titles, legend, limits.

The grid splits the data; the function draws each piece.

1 / 2

Your first grid

Create the grid with the faceting variables, then call map_dataframe() with an axes-level function and column names as strings:

Python A FacetGrid with columns

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.

Notes on that code:

  • col_order fixes panel order (the default is the order data appears, or sorted for categories).
  • height and aspect size each panel; panel width is height * aspect.
  • set_titles("{col_name}") is a template; with rows you can use "{row_name} | {col_name}".
  • Axes are shared across panels by default (sharex=True, sharey=True), which keeps comparisons honest.

Rows, columns and hue together

Use all three dimensions: row splits vertically, col splits horizontally, hue colours inside each panel. Call add_legend() when you use hue.

Python Rows, columns and hue

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.

margin_titles=True puts the row label on the right edge instead of repeating it above every panel, which saves space.

map versus map_dataframe

FacetGrid has two mapping methods:

Method Call style Use for
g.map_dataframe(func, x="a", y="b") Column names as keywords, data passed as data= Seaborn functions (recommended)
g.map(func, "a", "b") Column names as positional strings; func receives the arrays Matplotlib functions like plt.scatter, plt.hist

With map() the function is called once per panel and per hue level, with color and label keywords that hue handling relies on. With map_dataframe() seaborn gets the sub-DataFrame, so hue works automatically.

Python map() with a Matplotlib function

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 loop adds a mean line to each panel. g.axes is a 2D NumPy array of Axes, and g.axes.flat iterates over it in row-major order. That matches the order of groupby here only because the groups are sorted the same way; when in doubt, use g.axes_dict, a dictionary from facet name to axes.

Custom functions

You can map your own function. It receives the data for the panel, and receives color, so it plays well with hue:

Python A custom panel function

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.

Wrapping, limits and tidying

  • col_wrap=3 wraps a single faceting variable into rows of three, ideal for many categories (do not combine with row).
  • sharex=False / sharey=False free the axes when scales differ wildly. Say so in the caption, because panels no longer compare directly.
  • g.set(xlim=(0, 60), xticks=[0, 30, 60]) sets properties on every panel at once.
  • g.tight_layout() and g.figure.subplots_adjust(top=0.9) fix cramped titles.
  • g.refline(y=0) draws a reference line on every panel (seaborn 0.12+).
  • g.savefig("grid.png", dpi=200) saves the whole grid.

Common mistakes

  • Passing figsize: use height and aspect.
  • Forgetting add_legend() when hue is set.
  • Using map() with column names as keywords for seaborn functions; use map_dataframe().
  • Too many panels: past about 12, aggregate or filter instead.

The same ideas appear in the shortcut figure-level functions; next you meet the grid that plots variables against each other, PairGrid. For a plain Matplotlib approach, see subplots.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which seaborn functions are built on FacetGrid?

    The figure-level functions create a FacetGrid, which is why they accept col, row and hue.
  2. 2. How do you size a FacetGrid?

    height is the height of each panel in inches and aspect is width divided by height.
  3. 3. Which method should you use to draw a seaborn scatterplot on every panel?

    map_dataframe passes each sub-DataFrame to the function so keyword column names and hue work.
  4. 4. A single categorical variable has 9 levels. What argument arranges the panels in a tidy block?

    col_wrap wraps one faceting variable onto several rows.

Summary

  • FacetGrid(data, row=, col=, hue=) splits the data; map_dataframe or map draws each piece.
  • Panels share axes by default; size them with height and aspect.
  • set_titles, set_axis_labels, add_legend, refline and set polish the grid.
  • col_wrap handles one variable with many levels.

Next: Pair Plots and PairGrid