Bird distributionsΒΆ

Visualize the dataset

import pandas as pd
import matplotlib.pyplot as plt
birds = pd.read_csv('../../../data/birds.csv')
birds.head()

Show a histogram of the MaxBodyMass data

birds['MaxBodyMass'].plot(kind = 'hist',bins = 10,figsize = (12,12))
plt.show()

Experiment with bins

birds['MaxBodyMass'].plot(kind = 'hist',bins = 30,figsize = (12,12))
plt.show()

Filter the data and create a new histogram

filteredBirds = birds[(birds['MaxBodyMass'] > 1) & (birds['MaxBodyMass'] < 60)]      
filteredBirds['MaxBodyMass'].plot(kind = 'hist',bins = 40,figsize = (12,12))
plt.show()        

Create a 2D histgram showing the relationship between MaxBodyMass and MaxLength

from matplotlib import colors
from matplotlib.ticker import PercentFormatter

x = filteredBirds['MaxBodyMass']
y = filteredBirds['MaxLength']

fig, ax = plt.subplots(tight_layout=True)
hist = ax.hist2d(x, y)

Working with the filtered dataset, create a labelled and stacked histogram superimposing ConservationStatus with MaxBodyMass

x1 = filteredBirds.loc[filteredBirds.ConservationStatus=='EX', 'MinWingspan']
x2 = filteredBirds.loc[filteredBirds.ConservationStatus=='CR', 'MinWingspan']
x3 = filteredBirds.loc[filteredBirds.ConservationStatus=='EN', 'MinWingspan']
x4 = filteredBirds.loc[filteredBirds.ConservationStatus=='NT', 'MinWingspan']
x5 = filteredBirds.loc[filteredBirds.ConservationStatus=='VU', 'MinWingspan']
x6 = filteredBirds.loc[filteredBirds.ConservationStatus=='LC', 'MinWingspan']

kwargs = dict(alpha=0.5, bins=20)

plt.hist(x1, **kwargs, color='red', label='Extinct')
plt.hist(x2, **kwargs, color='orange', label='Critically Endangered')
plt.hist(x3, **kwargs, color='yellow', label='Endangered')
plt.hist(x4, **kwargs, color='green', label='Near Threatened')
plt.hist(x5, **kwargs, color='blue', label='Vulnerable')
plt.hist(x6, **kwargs, color='gray', label='Least Concern')

plt.gca().set(title='Conservation Status', ylabel='Max Body Mass')
plt.legend();

Working with Seaborn, create a smooth plot about MinWingspan

import seaborn as sns
import matplotlib.pyplot as plt
sns.kdeplot(filteredBirds['MinWingspan'])
plt.show()

Try a kdeplot about MaxBodyMass

sns.kdeplot(filteredBirds['MaxBodyMass'])
plt.show()

Experiment with the plot smoothing parameter

sns.kdeplot(filteredBirds['MaxBodyMass'], bw_adjust=.2)
plt.show()

Create a 2D kdeplot comparing MinLength and MaxLength with hue showing ConservationStatus

sns.kdeplot(data=filteredBirds, x="MinLength", y="MaxLength", hue="ConservationStatus")