Statistical plots with Seaborn
Seaborn draws on Matplotlib but takes a DataFrame and column names, and it groups, averages and summarises the data for you: one call per chart.
With Matplotlib you pass lists of numbers (lesson 3.5); with Seaborn you pass a DataFrame as data= and name its columns. Seaborn draws on Matplotlib's axes, so plt.title() and plt.show() still apply.
A dataset to plot
import seaborn as sns
# Basic plotting with Seaborn
tips = sns.load_dataset('tips')
tips
OutputReal output
total_bill tip sex smoker day time size 0 16.99 1.01 Female No Sun Dinner 2 1 10.34 1.66 Male No Sun Dinner 3 2 21.01 3.50 Male No Sun Dinner 3 3 23.68 3.31 Male No Sun Dinner 2 4 24.59 3.61 Female No Sun Dinner 4 .. ... ... ... ... ... ... ... 239 29.03 5.92 Male No Sat Dinner 3 240 27.18 2.00 Female Yes Sat Dinner 2 241 22.67 2.00 Male Yes Sat Dinner 2 242 17.82 1.75 Male No Sat Dinner 2 243 18.78 3.00 Female No Thur Dinner 2 [244 rows x 7 columns]
load_dataset downloads one of Seaborn's sample datasets the first time (it needs an internet connection) and returns a DataFrame: 244 restaurant bills with the tip, the guests' sex, smoker or not, the day, lunch or dinner, and the party size.
Relationships: scatter and line plots
import matplotlib.pyplot as plt
# Create a scatter plot
sns.scatterplot(x='total_bill', y='tip', data=tips)
plt.title("Scatter Plot of Total Bill vs Tip")
plt.show()
OutputReal output
sns.lineplot(x='size', y='total_bill', data=tips)
plt.title("Line Plot of Total bill by size")
plt.show()
OutputReal output · the band varies slightly between runs
Each party size appears many times, so lineplot draws the mean bill per size. The shaded band is a 95% confidence interval: where the mean would probably fall with another sample of bills. Seaborn estimates it by resampling the data at random, so the band shifts a little between runs.
Categories: bar, box and violin plots
sns.barplot(x='day', y='total_bill', data=tips)
plt.title('Bar Plot of Total Bill By Day')
plt.show()
OutputReal output · the black lines vary slightly between runs
sns.boxplot(x="day", y='total_bill', data=tips)
OutputReal output
<Axes: xlabel='day', ylabel='total_bill'>
sns.violinplot(x='day', y='total_bill', data=tips)
OutputReal output
<Axes: xlabel='day', ylabel='total_bill'>
A bar shows the mean, with the same confidence interval as a black line. A box plot shows the spread: the box runs from the 25th to the 75th percentile with the median as a line inside, the whiskers reach the furthest values within 1.5 box lengths, and dots beyond them are outliers. A violin plot shows the same spread as a smooth outline, wide where many values lie.
The <Axes: ...> line is the cell's last value, the plot area Seaborn drew on; end with plt.show() to hide it.
Distributions: histogram and density
sns.histplot(tips['total_bill'], bins=10, kde=True)
OutputReal output
<Axes: xlabel='total_bill', ylabel='Count'>
sns.kdeplot(tips['total_bill'], fill=True)
OutputReal output
<Axes: xlabel='total_bill', ylabel='Density'>
kde=True adds a kernel density estimate: a smoothed version of the histogram. kdeplot draws only the curve; its height is a density, so the area under it is 1.
Every pair at once
sns.pairplot(tips)
OutputReal output · the address differs on each run
<seaborn.axisgrid.PairGrid at 0x1556fa7dac0>

pairplot draws a scatter plot for every pair of numeric columns and a histogram of each column on the diagonal: a first look at a new dataset.
Correlation heatmap
corr = tips[['total_bill', 'tip', 'size']].corr()
corr
OutputReal output
total_bill tip size total_bill 1.000000 0.675734 0.598315 tip 0.675734 1.000000 0.489299 size 0.598315 0.489299 1.000000
sns.heatmap(corr, annot=True, cmap='coolwarm')
OutputReal output
<Axes: >
A correlation runs from -1 to 1: near 1, two columns rise together; near 0 they are unrelated; near -1 one falls as the other rises. Bills and tips correlate at 0.68. annot=True writes the numbers in the squares, and cmap picks the colours.
Which plot for what
| Question | Seaborn call |
|---|---|
| How do two numbers relate? | scatterplot, lineplot |
| How does a number differ between groups? | barplot (mean), boxplot, violinplot (spread) |
| How is one number distributed? | histplot, kdeplot |
| A first look at every pair | pairplot |
| Which columns move together? | heatmap of df.corr() |
Practice
Practice 1Sales totals. With sales_data.csv (lesson 3.2), plot the total revenue per product category, then per region, as Seaborn bar plots of 10 by 6 inches with a title and axis labels.Show solution
import pandas as pd
sales_df = pd.read_csv('sales_data.csv')
# Plot total sales by product
plt.figure(figsize=(10, 6))
sns.barplot(x='Product Category', y="Total Revenue", data=sales_df, estimator=sum)
plt.title('Total Sales by Product')
plt.xlabel('Product')
plt.ylabel('Total Sales')
plt.show()
OutputReal output · the black lines vary slightly between runs
# Plot total sales by region
plt.figure(figsize=(10, 6))
sns.barplot(x='Region', y="Total Revenue", data=sales_df, estimator=sum)
plt.title('Total Sales by Region')
plt.xlabel('Region')
plt.ylabel('Total Sales')
plt.show()
OutputReal output · the black lines vary slightly between runs
estimator=sum makes each bar the total instead of the mean. The black lines are still confidence intervals, computed for the totals; errorbar=None leaves them out.