Add legend
plt.plot(women_degrees['Year'], women_degrees['Biology'], c='blue')
plt.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green')
plt.title("Percentage of Biology Degrees Awarded By Gender")
plt.legend(['Women', 'Men'], loc = "upper right")
plt.show()
Using ax.plot and turndown tick_params
fig, ax = plt.subplots()
ax.plot(women_degrees['Year'], women_degrees['Biology'], c='blue')
ax.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green')
ax.set_title("Percentage of Biology Degrees Awarded By Gender")
ax.legend(['Women', 'Men'], loc = "upper right")
ax.tick_params(bottom="off", top="off", left="off", right="off")
slist = ax.spines # remove all spines
for item in slist:
ax.spines[item].set_visible(False)
plt.show()
Change color of lines
Set color like: some_color = (12/255, 10/255, 103/255)
then set some_color to the line.
fig = plt.figure(figsize=(12, 12))
dark_blue = (0/255, 107/255, 164/255) # set color
orange = (255/255, 128/255, 14/255) # set color
for sp in range(0,4):
ax = fig.add_subplot(2,2,sp+1)
# The color for each line is assigned here.
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=dark_blue, label='Women', linewidth = 3)
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=orange, label='Men', linewidth = 3)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(major_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()
Make charts in a row
stem_cats = ['Engineering', 'Computer Science', 'Psychology', 'Biology', 'Physical Sciences', 'Math and Statistics']
fig = plt.figure(figsize=(18, 3))
for sp in range(0,6):
ax = fig.add_subplot(1,6,sp+1)
ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=3)
ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[sp]], c=cb_orange, label='Men', linewidth=3)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(stem_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()
Add annotations
fig = plt.figure(figsize=(18, 3))
for sp in range(0,6):
ax = fig.add_subplot(1,6,sp+1)
if sp == 0:
ax.text(2005, 87, "Men")
ax.text(2002, 8, "Women")
elif sp == 5:
ax.text(2005, 62, "Men")
ax.text(2001, 35, "Women")
else:
pass
ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=3)
ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[sp]], c=cb_orange, label='Men', linewidth=3)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(stem_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()
Another Exercise - Titanic
import pandas as pd
titanic = pd.read_csv("train.csv")
titanic = titanic[['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']] # select only the columns we want
print (titanic.head(5))
titanic = titanic.dropna(axis=0, how='any') # drops rows where there is any NaN
Seaborn with kernal density estimation
import seaborn as sns
import matplotlib.pyplot as plt
sns.distplot(titanic["Age"])
plt.show()
Draw the KDE line ONLY
sns.kdeplot(titanic["Age"], shade = True)
plt.xlabel("Age")
sns tweaking
sns.set_style("white")
sns.kdeplot(titanic["Age"], shade = True)
plt.xlabel("Age")
sns.despine(left = True, bottom=True)
Small Multiple
# Condition on unique values of the "Survived" column.
g = sns.FacetGrid(titanic, col="Survived", size=6)
# For each subset of values, generate a kernel density plot of the "Age" columns.
g.map(sns.kdeplot, "Age", shade=True)
OR
g = sns.FacetGrid(titanic, col = 'Pclass', size = 6) # here 6 means 6 inches in height
g.map(sns.distplot, "Age")
sns.despine(left = True, bottom = True)
plt.show()
para "hue, add legend"
doc
g = sns.FacetGrid(titanic, col="Survived", row="Pclass", hue = "Sex", size = 3)
g.map(sns.kdeplot, "Age", shade = True).add_legend() # add legend
sns.despine(left=True, bottom=True)
plt.show()