Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Combining Regression and Grids | Section
Statistical Visualization with Seaborn

bookCombining Regression and Grids

The lmplot (linear model plot) is a figure-level function that combines regplot and FacetGrid.

While regplot is great for a single relationship, lmplot allows you to compare linear relationships across different categories. You can separate data by color (hue) or by splitting it into different subplots (col/row), making it powerful for answering questions like "Does the relationship between bill and tip change if the customer is a smoker?" .

Key Parameters

  • hue: separating the data by color and drawing a separate regression line for each group;
  • col / row: separating the data into distinct subplots;
  • markers: a list of symbols to distinguish groups visually (e.g., ['o', 'x']), which is helpful for accessibility.

Example

Here we compare the tips given during Lunch vs. Dinner. Notice how col splits the view, while hue compares smokers within each view.

1234567891011121314151617
import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('tips') # Create a grid of regression plots sns.lmplot( data=df, x='total_bill', y='tip', col='time', # Split: Lunch vs Dinner hue='smoker', # Color: Yes vs No palette='Set1' ) plt.show()
copy
Task

Swipe to start coding

Analyze the tips dataset to see how gender and smoking status affect tipping behavior.

  1. Set the style to 'darkgrid'. Set the figure background color to 'lightpink'.
  2. Create an lmplot using the tips dataset (df):
    • Map 'total_bill' to x and 'tip' to y.
    • Color the lines based on 'smoker' status (hue).
    • Split the visualization into columns based on 'sex' (col).
    • Use distinct markers: 'o' for the first group and 'x' for the second.
    • Use the 'crest' palette.
  3. Display the plot.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 22
single

single

Ask AI

expand

Ask AI

ChatGPT

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

close

bookCombining Regression and Grids

Swipe to show menu

The lmplot (linear model plot) is a figure-level function that combines regplot and FacetGrid.

While regplot is great for a single relationship, lmplot allows you to compare linear relationships across different categories. You can separate data by color (hue) or by splitting it into different subplots (col/row), making it powerful for answering questions like "Does the relationship between bill and tip change if the customer is a smoker?" .

Key Parameters

  • hue: separating the data by color and drawing a separate regression line for each group;
  • col / row: separating the data into distinct subplots;
  • markers: a list of symbols to distinguish groups visually (e.g., ['o', 'x']), which is helpful for accessibility.

Example

Here we compare the tips given during Lunch vs. Dinner. Notice how col splits the view, while hue compares smokers within each view.

1234567891011121314151617
import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('tips') # Create a grid of regression plots sns.lmplot( data=df, x='total_bill', y='tip', col='time', # Split: Lunch vs Dinner hue='smoker', # Color: Yes vs No palette='Set1' ) plt.show()
copy
Task

Swipe to start coding

Analyze the tips dataset to see how gender and smoking status affect tipping behavior.

  1. Set the style to 'darkgrid'. Set the figure background color to 'lightpink'.
  2. Create an lmplot using the tips dataset (df):
    • Map 'total_bill' to x and 'tip' to y.
    • Color the lines based on 'smoker' status (hue).
    • Split the visualization into columns based on 'sex' (col).
    • Use distinct markers: 'o' for the first group and 'x' for the second.
    • Use the 'crest' palette.
  3. 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Β 22
single

single

some-alt