Profile picture

2.7 - Intro to Seaborn

Last updated: February 16th, 20192019-02-16Project preview

rmotr


Intro to Seaborn - Exercises

In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline

green-divider

Exercise 1

Import the data/tips.csv dataset.

In [ ]:
# your code goes here
In [ ]:
df = pd.read_csv('data/tips.csv')

df.head()

green-divider

Exercise 2

Plot an histogram of the total_bill column. Add title, xlabel and ylabel.

In [ ]:
# your code goes here
In [ ]:
plot = sns.distplot(df['total_bill'])

plot.set(title='Total bill',
         xlabel='Value',
         ylabel='Frequency')

green-divider

Exercise 3

Plot a scatter showing the relationship between total_bill and tip.

In [ ]:
# your code goes here
In [ ]:
sns.jointplot(x='total_bill', y='tip', data=df,
              kind='scatter')

green-divider

Exercise 4

Plot a scatter showing all the relationships between total_bill, tip and size.

In [ ]:
# your code goes here
In [ ]:
sns.pairplot(df, vars=['total_bill', 'tip', 'size'])

green-divider

Exercise 5

Draw a scatter plot showing the relationship between total_bill and the categorical variabledays.

In [ ]:
# your code goes here

Remember to use stripplot() as one variable is categorical.

In [ ]:
sns.stripplot(data=df, x='day', y='total_bill')

green-divider

Exercise 6

Draw a scatter plot showing the relationship between tip and the categorical variable day, differ the dots by sex.

In [ ]:
# your code goes here

Remember that we can use hue parameter indicating by which column we want to differ the dots.

In [ ]:
sns.stripplot(data=df, x='tip', y='day', hue='sex')

green-divider

Exercise 7

Draw a boxplot of total_bill values per day .

In [ ]:
# your code goes here
In [ ]:
sns.boxplot(data=df, x='day', y='total_bill')

green-divider

Exercise 8

Draw two histograms plot of the tip values per day.

In [ ]:
# your code goes here

Remember that we can create multigraphic plots in Seaborn using FacetGrid() function.

In [ ]:
grid = sns.FacetGrid(df, col='time')

grid.map(plt.hist, 'tip')

green-divider

Exercise 9

Draw two scatter plots of the relation between total_bill and tip per sex. Also, differing the dots by smoker.

In [ ]:
# your code goes here
In [ ]:
grid = sns.FacetGrid(df, col='sex', hue='smoker')

grid.map(plt.scatter, 'total_bill', 'tip', alpha=0.8)

grid.add_legend()

purple-divider

Notebooks AI
Notebooks AI Profile20060