Skip to main content
Back to module
Data Visualization & Storytelling · Charts in Python

Statistical charts

Reading41mLesson 2 of 3 in this module

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 bins 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 plot shows the median (middle line), the interquartile range (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 band 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 coefficient 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.68

Values 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.
  • Overplotting. Thousands of opaque dots form a blob. Use alpha, smaller markers, or sns.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.

Also in this lesson: