Building Multi-Plot Grids
A FacetGrid is the fundamental structure that allows you to create a matrix of plots defined by categorical variables.
Think of it as a two-step process:
- Build the grid: you define the "skeleton" (rows and columns) based on your data. At this stage, the plots are empty;
- Map the data: you use the
.map()method to "paint" a specific type of plot (histogram, scatter, etc.) onto every cell in that grid.
The Workflow
# Step 1: Initialize the grid
# This creates empty subplots: one for each 'time' (Lunch/Dinner)
g = sns.FacetGrid(data=df, col='time')
# Step 2: Map a plot onto the grid
# This draws a histogram of 'total_bill' in every empty subplot
g.map(sns.histplot, 'total_bill')
Key Parameters
col/row: the variables that define the grid structure;height: height (in inches) of each individual facet (subplot);.map(func, *args, **kwargs):func: the plotting function to use (e.g.,sns.histplot,plt.scatter);*args: the column names to plot (e.g.,'total_bill');**kwargs: styling arguments (e.g.,color='red').
Task
Swipe to start coding
Analyze the distribution of total bills, breaking it down by day and smoking status.
- Set the style to
'whitegrid'. Set the background color to'cornsilk'('axes.facecolor'). - Initialize the FacetGrid (
g):- Use the
tipsdataset (df). - Create a column for each
'day'. - Create a row for each
'smoker'status. - Set the
heightof each subplot to3.
- Use the
- Map a histogram onto this grid:
- Use
sns.histplotas the plotting function. - Plot the
'total_bill'variable. - Set the
colorto'olive'. - Add a KDE curve (
kde=True). - Remove the bar filling (
fill=False) to see the outline better. - Set the
binwidthto4.
- Use
- Display the plot.
Solution
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 18
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.55
Building Multi-Plot Grids
Swipe to show menu
A FacetGrid is the fundamental structure that allows you to create a matrix of plots defined by categorical variables.
Think of it as a two-step process:
- Build the grid: you define the "skeleton" (rows and columns) based on your data. At this stage, the plots are empty;
- Map the data: you use the
.map()method to "paint" a specific type of plot (histogram, scatter, etc.) onto every cell in that grid.
The Workflow
# Step 1: Initialize the grid
# This creates empty subplots: one for each 'time' (Lunch/Dinner)
g = sns.FacetGrid(data=df, col='time')
# Step 2: Map a plot onto the grid
# This draws a histogram of 'total_bill' in every empty subplot
g.map(sns.histplot, 'total_bill')
Key Parameters
col/row: the variables that define the grid structure;height: height (in inches) of each individual facet (subplot);.map(func, *args, **kwargs):func: the plotting function to use (e.g.,sns.histplot,plt.scatter);*args: the column names to plot (e.g.,'total_bill');**kwargs: styling arguments (e.g.,color='red').
Task
Swipe to start coding
Analyze the distribution of total bills, breaking it down by day and smoking status.
- Set the style to
'whitegrid'. Set the background color to'cornsilk'('axes.facecolor'). - Initialize the FacetGrid (
g):- Use the
tipsdataset (df). - Create a column for each
'day'. - Create a row for each
'smoker'status. - Set the
heightof each subplot to3.
- Use the
- Map a histogram onto this grid:
- Use
sns.histplotas the plotting function. - Plot the
'total_bill'variable. - Set the
colorto'olive'. - Add a KDE curve (
kde=True). - Remove the bar filling (
fill=False) to see the outline better. - Set the
binwidthto4.
- Use
- Display the plot.
Solution
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 18
single