Module 6 · Seaborn · Lesson 30 of 66
Distribution plots
See how values spread out: histograms, density curves, joint plots and pair plots.
In this lesson · about 16 minutes
- Load Seaborn's sample data sets
- Draw a histogram with a density curve using histplot
- Compare two columns with jointplot and scan a whole table with pairplot
Seaborn sits on top of Matplotlib and makes good-looking statistical charts in one line. You hand it a DataFrame and the names of the columns you care about, and it handles the grouping, colors, labels and legend for you.
It ships with a few small practice data sets. We'll use tips a lot: one row per restaurant bill, with the bill total, the tip, and facts about the table such as the day and party size.
One column: histplot
A histogram cuts the range of a column into bins and counts the rows in each. bins sets how many bins, and kde=True draws a smoothed estimate of the shape on top.
Exercise
Histogram of tips
Draw a histogram of the tip column with 20 bins and the density curve turned on.
Code that runs before yours
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')Two columns: jointplot
sns.jointplot(x=..., y=..., data=...) draws a scatter plot of two columns with a histogram of each along the edges. Change the middle with kind: 'scatter' (the default), 'hex' for hexagon bins that show where points pile up, 'reg' to add a regression line, or 'kde' for density contours.
Exercise
Hexagon joint plot
Make a joint plot with total_bill on the x axis and tip on the y axis, using hexagon bins.
Code that runs before yours
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')Every pair at once: pairplot
sns.pairplot(df) draws a scatter plot for every pair of number columns and a histogram of each column on the diagonal. Add hue='column' to color the points by a category. It's a fast first look at a new data set.
Rug and KDE plots
A rug plot puts a small tick at every value, so you see each data point. A KDE (kernel density estimate) replaces each point with a small bell curve and adds them all up into one smooth curve. That's where the curve from kde=True comes from.
Exercises solved: 0 of 2
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.