0
0
Matplotlibdata~10 mins

Small multiples (facet grid) in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Small multiples (facet grid)
Load data
Choose variable to facet by
Create grid layout
Plot each subset in its grid cell
Display all plots together
We split data by a variable, create a grid, and plot each subset in its own small plot, showing all plots together.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

sns.set()
data = sns.load_dataset('tips')
g = sns.FacetGrid(data, col='time')
g.map(plt.hist, 'total_bill')
plt.show()
This code loads a dataset, creates a facet grid by 'time', plots histograms of 'total_bill' for each time, and shows the plots.
Execution Table
StepActionEvaluationResult
1Load dataset 'tips'Data loaded with columns including 'time' and 'total_bill'DataFrame with 244 rows
2Create FacetGrid with col='time'Unique 'time' values: ['Lunch', 'Dinner']Grid with 2 columns for Lunch and Dinner
3Map histogram of 'total_bill' to each facetFor Lunch: histogram bins calculated For Dinner: histogram bins calculatedTwo histograms plotted separately
4Show plotAll facets displayed side by sideWindow with 2 histograms labeled Lunch and Dinner
💡 All subsets plotted; visualization complete
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
dataNoneDataFrame loadedSameSameSame
gNoneNoneFacetGrid object with 2 facetsSameSame
Key Moments - 2 Insights
Why do we see two separate plots instead of one combined plot?
Because the FacetGrid splits data by the 'time' variable into Lunch and Dinner subsets, then plots each subset in its own grid cell as shown in execution_table step 2 and 3.
What does the map function do in the FacetGrid?
It applies the plotting function (here plt.hist) to each subset of data in the grid cells, creating separate histograms as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many facets does the grid have after step 2?
A1
B2
C3
D4
💡 Hint
Check execution_table row for step 2 showing unique 'time' values and grid columns
At which step are the histograms for each subset created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row describing mapping histograms to facets
If we changed col='time' to col='day', what would change in the execution table?
ANumber of facets in step 2 would change to unique days
BHistograms would plot 'time' instead of 'total_bill'
CData would be loaded differently in step 1
DNo change at all
💡 Hint
FacetGrid splits data by the column specified in step 2
Concept Snapshot
Small multiples (facet grid) split data by a variable.
Create a grid layout with one plot per subset.
Use FacetGrid in seaborn to manage this.
Map plotting functions to each facet.
Shows multiple small plots side by side for easy comparison.
Full Transcript
Small multiples or facet grids help us see data split by a category. We load data, pick a variable to split by, then create a grid with one plot per category. Each plot shows data only for that category. In the example, we load the 'tips' dataset, split by 'time' (Lunch or Dinner), and plot histograms of total bills for each. The FacetGrid creates two plots side by side. This helps compare distributions easily. The map function applies the histogram to each subset. Finally, we show all plots together. This method is great to compare groups visually.