import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
births = pd.read_csv('baby.csv')
births.head()
births.shape
We often use bar plots to display distributions of a categorical variable:
births['Maternal Smoker'].value_counts()
births['Maternal Smoker'].value_counts().plot(kind = 'bar');
Note: putting a semicolon after a plot call hides all of the unnecessary text that comes after it (the <matplotlib.axes_....>
).
sns.countplot(births['Maternal Smoker']);
But we can also use them to display a numerical variable that has been measured on individuals in different categories.
# These are made up!
majors = ['Data Science', 'History', 'Biology', 'Business']
gpas = [3.35, 3.20, 2.98, 3.51]
# What if we change bar to barh?
plt.bar(majors, gpas);
sns.barplot(majors, gpas);
Used for visualizing a single quantitative variable. Rug plots show us each and every value.
bweights = births["Birth Weight"]
bweights
sns.rugplot(bweights);
Our old friend!
# By default, you get some arbitrary bins. We usually like to pick our own.
plt.hist(bweights);
min(bweights), max(bweights)
bw_bins = range(50, 200, 5)
plt.hist(bweights, bins=bw_bins, ec='w');
plt.hist(bweights, density=True, bins=bw_bins, ec='w');
# alternative way of getting this plot
bweights.plot(kind = 'hist', density=True, bins=bw_bins, ec='w');
Increasing bin width loses granularity, but this may be fine for our purposes.
plt.hist(bweights, bins = np.arange(50, 200, 20), density=True, ec='w');
The bin widths don't all need to be the same!
plt.hist(bweights, bins = [50, 100, 120, 140, 200], density=True, ec='w');
sns.distplot(bweights);
Can isolate the histogram:
sns.distplot(bweights, hist=False);
Can even show a rugplot with it!
sns.distplot(bweights, hist=False, rug=True);
plt.figure(figsize = (3, 6))
sns.boxplot(bweights, orient='v'); # the orient argument makes this vertical, to be consistent with the side by side ones
q1 = np.percentile(bweights, 25)
q2 = np.percentile(bweights, 50)
q3 = np.percentile(bweights, 75)
iqr = q3 - q1
whisk1 = q1 - 1.5*iqr
whisk2 = q3 + 1.5*iqr
whisk1, q1, q2, q3, whisk2
plt.figure(figsize = (3, 6))
sns.violinplot(bweights, orient='v');
sm_bweights = births[births['Maternal Smoker'] == True]['Birth Weight']
nsm_bweights = births[births['Maternal Smoker'] == False]['Birth Weight']
sns.distplot(nsm_bweights, bins=bw_bins, hist_kws=dict(ec='w'), label='non smoker');
sns.distplot(sm_bweights, bins=bw_bins, hist_kws=dict(ec='w'), label='smoker');
plt.legend();
sns.distplot(nsm_bweights, bins=bw_bins, hist=False, hist_kws=dict(ec='w'), label='non smoker');
sns.distplot(sm_bweights, bins=bw_bins, hist=False, hist_kws=dict(ec='w'), label='smoker');
plt.legend();
# Alpha adjusts transparency of the bins
plt.hist(nsm_bweights, bins = bw_bins, ec='w', density=True, alpha=0.4, label='non smoker');
plt.hist(sm_bweights, bins = bw_bins, ec='w', density=True, alpha=0.4, label='smoker');
plt.xlabel('Birth Weight')
plt.legend();
plt.figure(figsize=(5, 8))
sns.boxplot(data=births, x = 'Maternal Smoker', y = 'Birth Weight');
plt.figure(figsize=(5, 8))
sns.violinplot(data=births, x = 'Maternal Smoker', y = 'Birth Weight');
A less fancy version of the above two plots:
two_distributions = [nsm_bweights.values, sm_bweights.values]
groups = ['non-smokers', 'smokers']
plt.boxplot(two_distributions, labels=groups);
plt.violinplot(two_distributions);
births
plt.scatter(births['Maternal Height'], births['Birth Weight'])
plt.xlabel('Maternal Height')
plt.ylabel('Birth Weight');
births['Birth Weight']
births['Maternal Height']
sns.scatterplot(data = births, x = 'Maternal Height', y = 'Birth Weight', hue = 'Maternal Smoker');
sns.lmplot(data = births, x = 'Maternal Height', y = 'Birth Weight', ci=False);
sns.jointplot(data = births, x = 'Maternal Height', y = 'Birth Weight');
sns.jointplot(data = births, x = 'Maternal Height', y = 'Birth Weight', kind='hex');
sns.jointplot(data = births, x = 'Maternal Height', y = 'Birth Weight', kind='kde');
Calling .plot()
results in weird things!
births.plot();