Lab 14 — Seaborn: Statistical Plots¶
Session: Week 7 · Session 14 · 90 min CLO: CLO-1 Difficulty: Intermediate
Learning objectives¶
By the end of this lab you can:
- Use
relplot,catplot, anddisplotwithkindandhue. - Read boxplots, violin plots, heatmaps, and pairplots.
- Choose the right plot for a three-variable question.
- Interpret what a plot shows — and what it hides.
Problem statement¶
The marketing team asks three questions about the tips data: (1) does the
bill–tip relationship differ between lunch and dinner? (2) which day has
the highest tips accounting for bill size? (3) which variables correlate
most with tip? Your deliverable is three plots, each answering exactly
one question, plus one sentence of interpretation each.
Dataset requirements¶
Seaborn built-in tips (244 rows). Optional: penguins for the challenge.
Step-by-step tasks¶
- Q1 — relationship by meal:
sns.relplot(data=tips, x="total_bill", y="tip", hue="time", style="smoker", alpha=0.6). Interpret: does the slope look different for lunch vs. dinner? - Q2 — tip proportion by day: first add
tips["tip_pct"] = tips["tip"] / tips["total_bill"] * 100. Plotsns.boxplot(data=tips, x="day", y="tip_pct"). Which day's median is highest? Why istip_pctfairer than rawtipfor this question? - Q3 — correlations: build the numeric subset (
total_bill,tip,size) and plotsns.heatmap(df.corr(), annot=True, cmap="coolwarm"). Which single variable correlates most withtip? Report the number. - Add a categorical:
sns.catplot(data=tips, x="day", y="tip", hue="smoker", kind="bar", ci=None)— read it: is the smoker gap consistent across days? (Write one sentence.) - Pairplot for EDA:
sns.pairplot(tips[["total_bill", "tip", "size"]], diag_kind="kde")— name one pair with the clearest relationship and one with the weakest. - Style pass: set
sns.set_theme(style="whitegrid")at the top, and add titles viaplt.titleon the two single-axes plots.
Starter code¶
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
tips = sns.load_dataset("tips")
tips["tip_pct"] = tips["tip"] / tips["total_bill"] * 100
# Q1: relplot with hue="time", style="smoker"
# your code here
# Q2: boxplot of tip_pct by day
# your code here
# Q3: correlation heatmap with annotations
# your code here
# Q4: catplot bar, hue="smoker"
# your code here
# Q5: pairplot
# your code here
Expected output¶
- Q1: a relplot with both
timeandsmokerencoded; points overlap a lot but the positive trend is visible in both meals. - Q2: a boxplot; Thursday's median
tip_pctis highest (≈ 27%), Sunday's lowest (≈ 18%) — the opposite of the raw-tip ranking, which is the point. - Q3: heatmap with numbers;
total_billcorrelates withtipat ≈ 0.68. - Q4: a bar plot showing smokers tip slightly less per meal on most days.
- Q5: pairplot with 3×3 grid;
total_bill–tipclearest,sizepairs weakest (mostly discrete).
Questions¶
- Why use
tip_pctinstead of rawtipwhen comparing days? - What does
hueadd to a plot that color alone cannot? - A heatmap shows correlation 0.68 between bill and tip. Does that mean bill causes tip? Explain.
- What is the difference between a boxplot and a violin plot?
- When is a pairplot a bad idea (think: dataset size)?
Challenge task¶
Repeat Q2 with penguins: build sns.boxplot(data=penguins,
x="species", y="body_mass_g", hue="sex"). Then add dodge=False and note
how the interpretation changes. Finally, write one sentence: which plot
would you show a non-technical manager, and why?
Marking rubric¶
| Criteria | Max | Notes |
|---|---|---|
| Q1 relplot correct + interpretation | 4 | hue+style, sentence |
| Q2 boxplot + tip_pct rationale | 4 | correct day, reasoning |
| Q3 heatmap + correlation report | 3 | 0.68 stated |
| Q4 catplot + reading | 3 | sentence on smoker gap |
| Q5 pairplot pairs named | 3 | strongest + weakest |
| Style pass | 2 | theme + titles |
| Answers to questions | 3 | Q1, Q3, Q4 correct |
| Challenge: penguins boxplot | 4 | hue, dodge note, sentence |
| Total | 26 |