What you'll learn
- Plot distributions and relationships
- Choose a histogram bin width deliberately and start bins on round numbers
- Read a box plot's median, interquartile range and outliers
- Summarise a relationship with Pearson's r without reading it as cause
Use seaborn to plot distributions and relationships, and understand the bins and statistics behind each plot.
Why it matters
Many questions analysts get are about spread and association: how variable are delivery times, do longer distances mean longer deliveries, is one group different from another? seaborn is a Python library built on matplotlib that draws these statistical charts in one line, straight from a pandas DataFrame. It is fast for exploration and produces clean defaults. The catch is that it makes decisions for you (bin widths, smoothing, confidence bands) that you need to understand before you show the chart to anyone.
Setup
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("tips") # restaurant bills, tips, day, time, party size
sns.set_theme(style="whitegrid")seaborn functions take data= (a DataFrame) and column names as strings for x, y and hue. Each returns a matplotlib Axes, so you can still use ax.set_title and everything from the previous lesson.
Distributions
Histogram:
ax = sns.histplot(data=df, x="total_bill", binwidth=5)
ax.set(title="Most bills are between $10 and $25", xlabel="Total bill ($)", ylabel="Number of bills")A histogram splits the range into binsThe equal ranges a histogram splits the data into before counting how many values fall in each. and counts how many values fall in each. The bin width changes the story: very wide bins hide detail, very narrow bins show noise. seaborn picks a width automatically; set binwidth (or bins) yourself and try two or three values before choosing. Starting bins on round numbers (0, 5, 10) makes the chart easier to read, which is what you will compute in the activity.
Density (KDE) curve: sns.kdeplot(data=df, x="total_bill") draws a smoothed version. It is good for comparing shapes, but it can suggest values that do not exist (for example, negative bills near zero). Use histograms when your audience is not used to density plots.
Comparing groups: box plots and violin plots compare a distribution across categories.
ax = sns.boxplot(data=df, x="day", y="total_bill", order=["Thur", "Fri", "Sat", "Sun"])A box plotA chart showing the median, the interquartile range as a box, and outliers beyond 1.5 times the IQR. shows the median (middle line), the interquartile rangeThe range covering the middle 50% of values, drawn as the box in a box plot. (the box, the middle 50% of values) and outliers (dots beyond 1.5 times the IQR). It is compact, but it hides the number of observations and multi-peaked shapes; adding sns.stripplot(..., color="0.3", size=3) on top shows the raw points.
Relationships
Scatter plot:
ax = sns.scatterplot(data=df, x="total_bill", y="tip", hue="time", alpha=0.7)Each dot is one row. hue colours dots by a category; alpha makes overlaps visible.
Scatter with a fitted line:
ax = sns.regplot(data=df, x="total_bill", y="tip", scatter_kws={"alpha": 0.5})regplot adds a least-squares line and a shaded 95% confidence bandThe shaded area regplot draws around a fitted line, showing uncertainty in the line, not where individual points fall. for the line. Say what that band is if you present it; it is not a range for individual points.
Putting a number on it. The Pearson correlation coefficientA number r from -1 to 1 measuring how close points lie to a straight line. r runs from -1 to 1 and measures how close the points are to a straight line:
r = df["total_bill"].corr(df["tip"]) # about 0.68Values near 0 mean no linear relationship (there could still be a curved one, which is why you always look at the scatter plot). Correlation does not show cause: bigger bills and bigger tips both come from bigger parties.
Figure-level functions
sns.displot, sns.relplot and sns.catplot are "figure-level" versions that can split into panels with col= and row=. You will use them for small multiples in the next lesson.
Common mistakes
- Accepting default bins. Always try a few bin widths, and say what width you used.
- OverplottingSo many overlapping dots that they form a blob; reduced with transparency, smaller markers or a 2D histogram.. Thousands of opaque dots form a blob. Use
alpha, smaller markers, orsns.histplot(x=..., y=...)for a 2D histogram. - Reading the regression band as a prediction range. It shows uncertainty in the line, not where individual points will fall.
- Using r alone. Very different datasets can share the same r (Anscombe's quartet is the classic demonstration). Plot first.
- Box plots for tiny groups. With five observations, show the points instead.
Quick recap
- seaborn draws statistical charts from DataFrame columns and returns matplotlib Axes.
- Distributions:
histplot(choose the bin width),kdeplot,boxplot,violinplot. - Relationships:
scatterplot,regplot; summarise with Pearson's r, but always look at the plot.
Try it
In the Do tab, compute histogram bin edges, bin counts and a correlation coefficient with NumPy: the numbers behind a seaborn histogram and scatter plot.
Key terms (6)
- bins
- The equal ranges a histogram splits the data into before counting how many values fall in each.
- box plot
- A chart showing the median, the interquartile range as a box, and outliers beyond 1.5 times the IQR.
- interquartile range
- The range covering the middle 50% of values, drawn as the box in a box plot.
- Pearson correlation coefficient
- A number r from -1 to 1 measuring how close points lie to a straight line.
- Overplotting
- So many overlapping dots that they form a blob; reduced with transparency, smaller markers or a 2D histogram.
- confidence band
- The shaded area regplot draws around a fitted line, showing uncertainty in the line, not where individual points fall.