Skip to content
tezvyn:

Facet Grid: A Visual GROUP BY for Your Data

Source: seaborn.pydata.orgHardHow cards are made

Facet Grid: A Visual GROUP BY for Your Data

A Facet Grid is a visual GROUP BY. It creates a matrix of plots, each showing a different subset of your data, to compare relationships across categories. The footgun is forgetting to call .map() to draw the plots; the grid is empty on its own.

Why it exists

A single plot shows the relationship between two variables. But what if you need to see how that relationship changes across different segments of your data, like by customer type or region? Overlaying everything on one chart becomes a mess. Facet grids solve this by creating an organized grid of plots, one for each data subset, making comparisons clean and direct.

The mental model

A Facet Grid is a visual GROUP BY statement. You define categorical variables to split your dataset into subsets. Instead of calculating an aggregate like SUM() or AVG() for each subset, FacetGrid draws a complete plot for each one. The result is a matrix of "small multiples," letting you compare patterns across many conditions at a glance.

How it works

First, you initialize a FacetGrid object, passing in your dataset and specifying which columns should define the grid's row, col, and hue. This creates a figure with an empty grid of axes; it does not draw any data. The key second step is calling the .map() or .map_dataframe() method on this grid object. You pass a plotting function (like seaborn.scatterplot) and the column names to this method. FacetGrid then iterates through your data subsets, drawing the specified plot on the corresponding axis in the grid.

When to use it

Use FacetGrid when you need to understand how a relationship or distribution changes across different levels of two or three categorical variables. It is a core tool for exploratory data analysis (EDA). For example, plotting the distribution of test scores (hist) for each school (col) and grade level (row). The col_wrap parameter is a lifesaver when faceting on a single variable with many levels, preventing a single, unreadably long row of plots.

When not to use it

Avoid FacetGrid when your faceting variables have too many unique levels (e.g., a user_id column with thousands of IDs), as this creates an unreadable grid. For simpler tasks, high-level functions like seaborn.relplot() or displot() are better; they use FacetGrid internally but require less code for common use cases.

One canonical example

Given a dataset of restaurant tips, you want to see how the relationship between total_bill and tip changes by the day of the week and time of day (Lunch/Dinner). You would initialize g = seaborn.FacetGrid(data, row="time", col="day"). This creates the empty grid. Then, you would draw the plots by calling g.map(seaborn.scatterplot, "total_bill", "tip"). The result is a grid of scatter plots, each showing the bill-vs-tip relationship for a specific combination, like "Lunch on Friday".

Interview question

After initializing a FacetGrid object with a dataset and faceting variables, what is the essential next step to visualize the data?

  • a.Defining the specific plot type and its variables within the FacetGrid constructor.
  • b.Displaying the generated grid using a dedicated .show() or .render() command.
  • c.Aggregating the data for each subset using a function like SUM() or AVG().
  • d.Calling the .map() or .map_dataframe() method with a plotting function.Correct
Why?

The card explicitly states that after initializing the FacetGrid (which creates an empty grid), the "key second step is calling the .map() or .map_dataframe() method" to draw the plots. While FacetGrid is a "visual GROUP BY," it draws complete plots for each subset rather than calculating aggregates like SUM() or AVG(), making option C incorrect.

Just read this? Test yourself on what you have been reading.

Read the original → seaborn.pydata.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on data visualization — each one lists the topics its interview covers.

See open roles