Module 6 · Seaborn · Lesson 31 of 66
Categorical plots
Compare groups: bar, count, box, violin, strip and swarm plots.
In this lesson · about 18 minutes
- Summarize a number by group with barplot and count rows with countplot
- Show spread by group with boxplot and violinplot
- Split any of them by a second category with hue
When one column is a category (day, sex, smoker) and the other is a number, you usually want to compare the groups. Seaborn's categorical plots all take the same arguments: x, y, data, and optionally hue to split each group again by another category.
barplot and countplot
sns.barplot draws one bar per group. Its height is the mean by default; pass estimator= any function that turns a list into one number, such as np.median or np.std. The thin line on each bar is the uncertainty around that estimate.
sns.countplot just counts rows per group, so it only needs x.
Exercise
Count the bills by day
Use a count plot to show how many bills there are for each day.
Code that runs before yours
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')Exercise
A different estimator
Draw a bar plot of total_bill by sex, but make each bar the standard deviation instead of the mean.
Code that runs before yours
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')boxplot and violinplot
A box plot shows the middle half of the values as a box, the median as a line through it, and whiskers reaching out to the rest. Dots beyond the whiskers are unusual values (outliers).
A violin plot shows the same thing as a mirrored density curve, so you can see the full shape. With hue and split=True, each side of the violin is one of the two hue groups.
Exercise
Split violins
Make a violin plot of total_bill for each day, colored by sex, with the two sexes split onto the two sides of each violin.
Code that runs before yours
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset('tips')Every point: stripplot and swarmplot
A strip plot draws every row as a dot over its group, with a little random sideways jitter so they don't hide each other. A swarm plot nudges the dots so none overlap, which shows the shape like a violin but with real points. Swarms get slow with thousands of rows. Use dodge=True to put each hue group side by side.
Exercises solved: 0 of 3
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.