Profile picture

2.4 - Type of Plots

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

rmotr


Type of plots - Exercises

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

%matplotlib inline

green-divider

Exercise 1

Import the data/titanic.csv dataset.

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

df.head()

green-divider

Exercise 2

Draw a scatter plot showing Age vs Fare. Use alpha=0.5 to modify points opacity.

In [ ]:
# your code goes here
In [ ]:
plt.scatter(df['Age'], df['Fare'], alpha=0.5)

green-divider

Exercise 3

Draw a scatter plot showing Age vs Fare, using red dots for male and blue dots for female.

In [ ]:
# your code goes here
In [ ]:
colors = ['red' if x == 'male' else 'blue' for x in df['Sex']]

plt.scatter(df['Age'], df['Fare'], c=colors)

green-divider

Exercise 4

Draw a pie chart showing Sex proportion. Add the proportion number and sex labels within the plot.

In [ ]:
# your code goes here

To add proportion number use autopct parameter, to add sex labels use labels parameter.

In [ ]:
sex_count = df['Sex'].value_counts()

plt.pie(sex_count, autopct="%.2f%%", labels=sex_count.index)

plt.show()

green-divider

Exercise 5

Draw a histogram showing the Fare payed. Show 60 bins, and choose custom width and color.

In [ ]:
# your code goes here
In [ ]:
plt.hist(df['Fare'], bins=60, width=10, color='#3498db')

plt.show()

green-divider

Exercise 6

Draw a boxplot showing the Age of the passengers.

The Age column has some missing values, so before plotting remove them.

In [ ]:
# your code goes here
In [ ]:
plt.boxplot(df['Age'].dropna())

plt.show()

green-divider

Exercise 7

Draw a boxplot showing the Age of the passengers, grouped by Sex.

In [ ]:
# your code goes here
In [ ]:
df.boxplot(column='Age', by='Sex')

purple-divider

Notebooks AI
Notebooks AI Profile20060