Seaborn Exercises




Seaborn Exercises

Time to practice your new seaborn skills! Try to recreate the plots below (don't worry about color schemes, just the plot itself.

The Data

We will be working with a famous titanic data set for these exercises. Later on in the Machine Learning section of the course, we will revisit this data, and use it to predict survival rates of passengers. For now, we'll just focus on the visualization of the data with seaborn:


In [1]:
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
sns.set_style('whitegrid')

In [3]:
titanic = sns.load_dataset('titanic')

In [4]:
titanic.head()


Out[4]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True

In [5]:
titanic.shape


Out[5]:
(891, 15)

Exercises

Recreate the plots below using the titanic dataframe. There are very few hints since most of the plots can be done with just one or two lines of code and a hint would basically give away the solution. Keep careful attention to the x and y labels for hints.

Note! In order to not lose the plot image, make sure you don't code in the cell that is directly above the plot, there is an extra cell above that one which won't overwrite that plot!


In [6]:
sns.jointplot(x='fare', y='age', data=titanic)


Out[6]:
<seaborn.axisgrid.JointGrid at 0x11895a3c8>

In [41]:



Out[41]:
<seaborn.axisgrid.JointGrid at 0x11d0389e8>

In [9]:
sns.distplot(titanic['fare'], kde=False, hist=True)


Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x11cec4940>

In [44]:



Out[44]:
<matplotlib.axes._subplots.AxesSubplot at 0x11fc5ca90>

In [14]:
sns.boxplot(x='class', y='age', data=titanic)


Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x11cbf33c8>

In [45]:



Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x11f23da90>

In [16]:
sns.swarmplot(x='class', y='age', data=titanic)


Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x11d5bec18>

In [46]:



Out[46]:
<matplotlib.axes._subplots.AxesSubplot at 0x11f215320>

In [17]:
sns.countplot(x='sex', data=titanic)


Out[17]:
<matplotlib.axes._subplots.AxesSubplot at 0x11d7d9dd8>

In [47]:



Out[47]:
<matplotlib.axes._subplots.AxesSubplot at 0x11f207ef0>

In [20]:
sns.heatmap(titanic.corr())


Out[20]:
<matplotlib.axes._subplots.AxesSubplot at 0x11d7c6ba8>

In [48]:



Out[48]:
<matplotlib.text.Text at 0x11d72da58>

In [26]:
import matplotlib.pyplot as plt
fg = sns.FacetGrid(data=titanic ,col='sex')
fg.map(plt.hist, 'age')


Out[26]:
<seaborn.axisgrid.FacetGrid at 0x122c369e8>

In [49]:



Out[49]:
<seaborn.axisgrid.FacetGrid at 0x11d81c240>

Great Job!

That is it for now! We'll see a lot more of seaborn practice problems in the machine learning section!