Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Building Multi-Plot Grids | Section
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Statistical Visualization with Seaborn

bookBuilding 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:

  1. Build the grid: you define the "skeleton" (rows and columns) based on your data. At this stage, the plots are empty;
  2. 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.

  1. Set the style to 'whitegrid'. Set the background color to 'cornsilk' ('axes.facecolor').
  2. Initialize the FacetGrid (g):
    • Use the tips dataset (df).
    • Create a column for each 'day'.
    • Create a row for each 'smoker' status.
    • Set the height of each subplot to 3.
  3. Map a histogram onto this grid:
    • Use sns.histplot as the plotting function.
    • Plot the 'total_bill' variable.
    • Set the color to 'olive'.
    • Add a KDE curve (kde=True).
    • Remove the bar filling (fill=False) to see the outline better.
    • Set the binwidth to 4.
  4. Display the plot.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 18
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookBuilding 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:

  1. Build the grid: you define the "skeleton" (rows and columns) based on your data. At this stage, the plots are empty;
  2. 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.

  1. Set the style to 'whitegrid'. Set the background color to 'cornsilk' ('axes.facecolor').
  2. Initialize the FacetGrid (g):
    • Use the tips dataset (df).
    • Create a column for each 'day'.
    • Create a row for each 'smoker' status.
    • Set the height of each subplot to 3.
  3. Map a histogram onto this grid:
    • Use sns.histplot as the plotting function.
    • Plot the 'total_bill' variable.
    • Set the color to 'olive'.
    • Add a KDE curve (kde=True).
    • Remove the bar filling (fill=False) to see the outline better.
    • Set the binwidth to 4.
  4. Display the plot.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 18
single

single

some-alt