Module 6 · Seaborn · Lesson 36 of 66
Seaborn exercises: the Titanic
Recreate seven charts of the passengers on the Titanic.
In this lesson · about 20 minutes
- Choose the right Seaborn chart for a question
- Read axis labels to work out which columns a chart uses
Time to practice on a famous data set: the passengers of the Titanic. Each row is a person, with their age, the fare they paid, their class, their sex and whether they survived. We'll use this data again when we predict survival with machine learning.
Each exercise describes a chart. Most take one or two lines. The setup for every exercise already ran sns.set_style('whitegrid').
Exercise
1. Fare and age
Make a joint plot with fare on the x axis and age on the y axis.
Code that runs before yours
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
titanic = sns.load_dataset('titanic')Exercise
2. Fares paid
Draw a red histogram of fare with 30 bins and no density curve.
Code that runs before yours
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
titanic = sns.load_dataset('titanic')Exercise
3. Age by class
Make a box plot of age for each class. Color the boxes with the 'rainbow' palette (with recent Seaborn, also pass hue='class' and legend=False to color by group).
Code that runs before yours
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
titanic = sns.load_dataset('titanic')Exercise
4. Every passenger's age
Now show the same comparison as a swarm plot, with the 'Set2' palette. Use small dots, size=2, so they all fit.
Code that runs before yours
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
titanic = sns.load_dataset('titanic')Exercise
5. Men and women aboard
Count the passengers by sex.
Code that runs before yours
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
titanic = sns.load_dataset('titanic')Exercise
6. What goes together
Draw a heat map of the correlations between the number columns, with the 'coolwarm' color map, and give it the title 'titanic.corr()'.
Code that runs before yours
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
titanic = sns.load_dataset('titanic')Exercise
7. Ages of men and women
Use a FacetGrid with one column per sex and draw a Matplotlib histogram of age in each.
Code that runs before yours
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style('whitegrid')
titanic = sns.load_dataset('titanic')Module review0 of 4 correct
Quiz score: 0 of 4
Exercises solved: 0 of 7
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.