seaborn and interactive charts · lesson 6 of 10
Parts in this lesson
Asns.relplot(..., col="store", col_wrap=2)one panel per store, wrapped onto rows
Bg = sns.FacetGrid(df, row="a", col="b")an empty grid of panels
Cg.map_dataframe(sns.histplot, x="kilos")draw the same chart in every panel
Dcol_order=[...], g.set_titles("{col_name}")which panel goes where, and what it's called

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:

Facet splitter. All four stores in one scatter plot: 240 store-days, rain against kilos, coloured by store. Is there a pattern for any one store? It’s hard to tell; the colours sit on top of each other.01020304020406080100rain_mmkilosBatangasLipaTanauanRosario
1 of 4 All four stores in one scatter plot: 240 store-days, rain against kilos, coloured by store. Is there a pattern for any one store? It’s hard to tell; the colours sit on top of each other.

Now in seaborn. Run the cells.

rain.ipynb
  • 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, and col="store": one panel per store. col_wrap=2 wraps 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, and g.set_titles("{col_name}") titles each with just the store's name instead of store = 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.

grid.ipynb
  • 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=False lets 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 like rice at Batangas.
  • The last cell uses sns.catplot, the figure-level function for categories (7.3), with kind="bar" and col="item": the average day's sales by weekday, one panel per item. order= puts the days in week order, and col_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.

trial.ipynb

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:

trial.ipynb

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

You call sns.relplot(..., col="region") on data with 9 regions. How do you get a 3 × 3 grid instead of one long row?
Why do small multiples usually share their axes?
You have weekly sales for 60 products. What’s best for “which products sell most?”

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