Facets
Last updated
When one chart holds too many groups, split it: one small chart per group, side by side, on the same axes. Lesson 6.5 built small multiples by hand with a loop; seaborn does it with one argument. In this lesson you'll split a crowded chart by store, build a grid by two variables with FacetGrid, put the panels in an order that helps, and see when splitting goes too far.
By the end of this lesson you can
- Make small multiples with col=, row= and col_wrap= on relplot, displot and catplot
- Build a custom grid with FacetGrid and map_dataframe
- Order facets so the comparison you care about is easy
- Know when there are too many facets, and what to draw instead
Split a crowded chart
Do Tindahan stores sell less on rainy days? Here's every store-day, with the rain in that store's town. Step through what happens when the chart is split:
Now in seaborn. Run the cells.
- The first cell is the crowded chart: four stores' 240 points in one panel, told apart only by colour.
- The second is the same call with
sns.relplot, the figure-level scatter function, andcol="store": one panel per store.col_wrap=2wraps the panels onto rows of two, so four panels make a 2 × 2 grid instead of one long row.col_order=sets the order of the panels, andg.set_titles("{col_name}")titles each with just the store's name instead ofstore = Batangas. - Colour isn't needed any more: the panel says which store it is.
Most days had no rain at all, so each panel has a column of dots at 0 mm. Past that, in every store, the rainy days sit a little lower.
Every panel shares the same axes, which is what makes small multiples work: a dot's height means the same in every panel, so you can compare across them as easily as within one.
Grids, and their order
col= splits by one variable; row= adds a second, for a grid. When you want a chart in each panel that the figure-level functions don't make, build the grid yourself with FacetGrid. Run the cells.
- The first cell works out an order for the stores: by total kilos, biggest first. seaborn's default is the order the values first appear in the data, which here would be alphabetical, and alphabetical order is almost never what a reader needs.
sns.FacetGrid(sales, row="item", col="store", ...)makes an empty 3 × 4 grid: a row per item, a column per store.g.map_dataframe(sns.histplot, x="kilos", binwidth=2)then draws a histogram in each panel from that panel's rows.sharey=Falselets each panel have its own y-axis, since rice sells far more than oil; the x-axes are still shared.g.set_titles("{row_name} at {col_name}")writes titles likerice at Batangas.- The last cell uses
sns.catplot, the figure-level function for categories (7.3), withkind="bar"andcol="item": the average day's sales by weekday, one panel per item.order=puts the days in week order, andcol_order=the items.
Ordering is part of the message. Put the panels in an order that means something: biggest first, in time order, or grouped by region. Then a reader's eye can run along the row and see the pattern.
Wrong way: 32 tiny charts
Small multiples work while each panel has enough in it to see, and there are few enough panels to compare. Run the cell: a new item, tried in 32 barangays for five days each.
Thirty-two panels of five points each. Each panel is too small to read, and to compare barangays you have to remember 32 little zigzags, in the order of their names, which tells you nothing. The question was probably “where did the item sell best?”, and this chart can't answer it.
Now run this notebook. It makes the same data, then draws one chart instead:
It has a row per barangay, sorted by mean sales, with the five days as grey dots and the mean as a pink mark. Now the best and worst barangays are at the ends, and you can see how much each varied.
The check: facets are for a handful of groups, roughly 2 to 12, each with enough data to make a picture. With dozens of groups, sort them on one chart. With only a few points per group, draw the points, not a panel each.
Check what you learned
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.
- seaborn: Building structured multi-plot grids: FacetGrid, map and map_dataframe
- seaborn: seaborn.relplot: col, row, col_wrap and col_order
- seaborn: seaborn.catplot: the figure-level function for categorical charts
- Becker, Cleveland and Shyu, The Visual Design and Control of Trellis Display, Journal of Computational and Graphical Statistics 5(2), 1996: trellis displays: the idea behind facet grids