Module 6 · Seaborn · Lesson 34 of 66

Regression plots

Fit and draw a trend line with lmplot, by group and across panels.

In this lesson · about 10 minutes

  • Draw a scatter plot with a fitted line using lmplot
  • Split the fit by group with hue, col and row
  • Size the figure with height and aspect

sns.lmplot(x=..., y=..., data=...) draws a scatter plot and fits a straight line through it, with a shaded band showing how sure the fit is. It's a quick way to see whether one number tends to rise with another. Larger bills do get larger tips.

Like catplot, lmplot is a grid underneath, so col= and row= give one panel and one fit per group. col_wrap=2 wraps the columns onto new rows. Set the size with height (inches per panel) and aspect (width as a multiple of height). Older tutorials call height size.

Exercise

A fit for each day

Use lmplot to fit tip against total_bill with one panel per day, wrapped two panels per row, each panel 3 inches tall.

Code that runs before yours
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')

Exercises solved: 0 of 1

Tip: solve each exercise first. Press Check to test your code.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.