import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

%matplotlib inline
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42

Matplotlib styles and data ink

!ls
02-classwork.zip                     critiques.pdf
Matplotlib styles and data ink.ipynb data-ink.gif
NBA stats.xlsx                       scrabble-point-spread.csv
country-gdp-2014.csv                 scrabble-tournament.csv
country-gdp-timeseries.csv           timeuse.csv
critiques.md                         tufte.pptx
df = pd.read_csv("../country-gdp-2014.csv")
df.head()
Country Continent GDP_per_capita life_expectancy Population
0 Afghanistan Asia 663 54.863 22856302
1 Albania Europe 4195 74.200 3071856
2 Algeria Africa 5098 68.963 30533827
3 Angola Africa 2446 45.234 13926373
4 Antigua and Barbuda N. America 12738 73.544 77656
df.groupby("Continent")['life_expectancy'].median().plot()
<matplotlib.axes._subplots.AxesSubplot at 0x108a9cf60>

png

df.groupby("Continent")['life_expectancy'].median().plot(kind='barh')
<matplotlib.axes._subplots.AxesSubplot at 0x108bb10f0>

png

# ax comes from the plotting
ax = df.groupby("Continent")['life_expectancy'].median().plot(kind='barh')
ax.set_ylabel("")
<matplotlib.text.Text at 0x1091551d0>

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(10,5))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax)
ax.set_ylabel("")
<matplotlib.text.Text at 0x108f617b8>

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax)
ax.set_ylabel("")
ax.grid(True)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax)
ax.set_ylabel("")

# When plotting the grid, you can send it options!
ax.grid(color='r', linestyle='-', linewidth=2)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax)
ax.set_ylabel("")

# When plotting the grid, you can send it options!
ax.grid(color='MidnightBlue', linestyle='-', linewidth=0.5)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax)
ax.set_ylabel("")

# When plotting the grid, you can send it options!
ax.grid(color='MidnightBlue', linestyle=':', linewidth=0.5)
ax.set_axisbelow(True)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0)
ax.set_ylabel("")

# When plotting the grid, you can send it options!
ax.grid(color='MidnightBlue', linestyle=':', linewidth=0.5)
ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0)
ax.set_ylabel("")

# When plotting the grid, you can send it options!
ax.grid(color='MidnightBlue', linestyle=':', linewidth=0.5)
ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

plt.tick_params(
    axis='x',     # changes apply to the x-axis
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    bottom='off', # ticks along the bottom edge are off
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0)
ax.set_ylabel("")

# When plotting the grid, you can send it options!
ax.grid(color='MidnightBlue', linestyle=':', linewidth=0.5)

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

ax.set_xlim((0,79))
(0, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(color='MidnightBlue', linestyle=':', linewidth=0.5)
ax.yaxis.grid(color='pink', linestyle="-.", linewidth=5)

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

ax.set_xlim((0,79))
(0, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on


plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on

ax.set_xlim((0,79))
(0, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='on',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on



ax.set_xlim((0,79))
(0, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on



ax.set_xlim((0,79))
(0, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,7))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0, width=1)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on



ax.set_xlim((0,79))
(0, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,7))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0, width=0.5)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on



ax.set_xlim((0,79))
(0, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0, width=0.4)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on



ax.set_xlim((0,79))
(0, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0, width=0.4)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on

median = df['life_expectancy'].median()
ax.plot([median, median], [-1, 10], c='red', linestyle="-", linewidth=0.5)

ax.set_xlim((0,79))
(0, 79)

png

df['life_expectancy'].median()
70.04150000000001
# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0, width=0.4)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
# Get rid of this line, and the minor grid lines disappear
#ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on

median = df['life_expectancy'].median()
ax.plot([median, median], [-1, 10], c='red', linestyle="-", linewidth=0.5)

# Use ax.annotate to add text
ax.annotate(s="Median life expectancy, 70 years", xy=(71,0), color='darkred')

ax.set_xlim((0,79))
(0, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', ax=ax, linewidth=0, width=0.4)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
# Get rid of this line, and the minor grid lines disappear
#ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on

median = df['life_expectancy'].median()
ax.plot([median, median], [-1, 10], c='red', linestyle="-", linewidth=0.5)

# Use ax.annotate to add text
ax.annotate(s="Median life expectancy, 70 years", xy=(71,0), color='darkred')

ax.set_xlim((30,79))
(30, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', color='gray', ax=ax, linewidth=0, width=0.4)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
# Get rid of this line, and the minor grid lines disappear
#ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on

median = df['life_expectancy'].median()
ax.plot([median, median], [-1, 10], c='red', linestyle="-", linewidth=0.5)

# Use ax.annotate to add text
ax.annotate(s="Median life expectancy, 70 years", xy=(71,0), color='darkred')

ax.set_xlim((30,79))
(30, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', color=['gray', 'darkred', 'gray', 'gray', 'gray', 'darkred'], ax=ax, linewidth=0, width=0.4)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
# Get rid of this line, and the minor grid lines disappear
#ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on

median = df['life_expectancy'].median()
ax.plot([median, median], [-1, 10], c='red', linestyle="-", linewidth=0.5)

# Use ax.annotate to add text
ax.annotate(s="Median life expectancy, 70 years", xy=(71,0), color='darkred')

ax.set_xlim((30,79))
(30, 79)

png

# Initialize the plot first, take the ax
fig, ax = plt.subplots(figsize=(7,3))

# Pass the ax to the .plot function
df.groupby("Continent")['life_expectancy'].median().plot(kind='barh', color=['darkred', 'gray', 'gray', 'gray', 'darkred', 'gray'], ax=ax, linewidth=0, width=0.4)
ax.set_ylabel("")

# ax.xaxis.grid takes all the same options as ax.grid
# but only applies to one of the axes
ax.xaxis.grid(which="major", color='MidnightBlue', linestyle=':', linewidth=1)
# Turn on a minor grid, a smaller grid, a more frequent grid
ax.xaxis.grid(which="minor", color='darkred', linestyle=":", linewidth=0.5)
# Get rid of this line, and the minor grid lines disappear
#ax.minorticks_on()

ax.set_axisbelow(True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labeltop='on', # top label is on
    labelbottom='on')  # bottom label is on

plt.tick_params(
    which='minor', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labeltop='off', # top label is on
    labelbottom='off')  # bottom label is on

median = df['life_expectancy'].median()
ax.plot([median, median], [-1, 10], c='red', linestyle="-", linewidth=0.5)

# Use ax.annotate to add text
ax.annotate(s="Median life expectancy, 70 years", xy=(71,0), color='darkred')

ax.set_xlim((30,79))
plt.savefig("life.pdf", transparent=True)

png

FiveThirtyEight scatterplot of countries’ GDP and life expectancy

fig, ax = plt.subplots()

# I already made a chart, I'm going to send it to you
df.plot(kind='scatter', x='GDP_per_capita', y='life_expectancy', ax=ax)
<matplotlib.axes._subplots.AxesSubplot at 0x10ac20208>

png

df[df['Continent'] == 'Africa'].plot(kind='scatter', x='GDP_per_capita', y='life_expectancy')
<matplotlib.axes._subplots.AxesSubplot at 0x10af09860>

png

df[df['Continent'] == 'Africa'].plot(kind='scatter', x='GDP_per_capita', y='life_expectancy')
df[df['Continent'] == 'Asia'].plot(kind='scatter', x='GDP_per_capita', y='life_expectancy')
df[df['Continent'] == 'Oceania'].plot(kind='scatter', x='GDP_per_capita', y='life_expectancy')
<matplotlib.axes._subplots.AxesSubplot at 0x10b0e44a8>

png

png

png

fig, ax = plt.subplots()
df[df['Continent'] == 'Africa'].plot(color="red", kind='scatter', x='GDP_per_capita', y='life_expectancy', ax=ax)
df[df['Continent'] == 'Asia'].plot(color="blue", kind='scatter', x='GDP_per_capita', y='life_expectancy', ax=ax)
df[df['Continent'] == 'Oceania'].plot(color="green", kind='scatter', x='GDP_per_capita', y='life_expectancy', ax=ax)
<matplotlib.axes._subplots.AxesSubplot at 0x10a6bb240>

png

fig, ax = plt.subplots()

for continent, selection in df.groupby("Continent"):
    #selection.plot(kind='scatter', x='GDP_per_capita', y='life_expectancy', ax=ax, label=continent)
    ax.plot(selection['GDP_per_capita'], selection['life_expectancy'], label=continent)

png

selection['life_expectancy']
5      73.822
19     62.994
22     70.261
33     77.010
35     71.026
48     73.353
69     63.290
130    70.073
131    70.506
158    67.851
179    74.777
182    72.432
Name: life_expectancy, dtype: float64
fig, ax = plt.subplots()

for continent, selection in df.groupby("Continent"):
    ax.plot(selection['GDP_per_capita'], selection['life_expectancy'], label=continent, marker='o', linestyle='')

png

fig, ax = plt.subplots()
fig.set_facecolor('lightgray')
ax.set_axis_bgcolor('lightgray')

for continent, selection in df.groupby("Continent"):
    ax.plot(selection['GDP_per_capita'], selection['life_expectancy'], label=continent, marker='o', linestyle='', markeredgewidth=0)

png

fig, ax = plt.subplots()
fig.set_facecolor('lightgray')
ax.set_axis_bgcolor('lightgray')

ax.set_prop_cycle('color', ['pink', 'purple', 'red', 'deeppink', 'lightcoral', 'magenta' ])

ax.grid(linestyle='-')
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)

plt.tick_params(
    which='major', # both major and minor ticks are affected
    top='off',   # ticks along the top edge are off
    left='off', # ticks along the right edge are off
    right='off', # ticks along the right edge are off
    bottom='off', # ticks along the bottom edge are on
    labelright='off',
    labelleft='on',
    labeltop='off', # top label is on
    labelbottom='on')  # bottom label is on

for continent, selection in df.groupby("Continent"):
    ax.plot(selection['GDP_per_capita'], selection['life_expectancy'], label=continent, marker='o', linestyle='', markeredgewidth=0)
    
ax.legend(loc='lower right')
ax.set_ylim((30, 85))
(30, 85)

png