VOTER PARTICIPATION
Does a person's age correlate with whether they'll vote? Let's look at a few visualizations to learn more:
What is the voter participation percentage over time? We want to look at this metric broken down by age groups, as well as the overall total.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
voters = pd.read_excel('1964-2022_votingdata_workingcopy.xlsx')voters.info()
voters.head()voters = voters[['year', 'age_group', 'tot_part', 'pop', 'male', 'female']]
voters.info()
voters.head()pres_years = [2020, 2016, 2012, 2008, 2004, 2000, 1996, 1992, 1988, 1984, 1980, 1976, 1972, 1968, 1964]
voters = voters[voters['year'].isin(pres_years)]
votersvoters = voters.set_index(['year', 'age_group'])
voters.head()voters = voters.sort_values(by=['year', 'age_group'])
votersTime Series Plot
Here is a line graph showing each age group, as well as the total voter participation percentages from the 1964 presidential election through the 2020 presidential election.
fig, ax = plt.subplots(figsize=(12,6))
sns.lineplot(data=voters, x='year', y='tot_part', hue='age_group')
ax.set_title('Voter Participation Time Series by Age Group', y=1.05)
ax.spines[['left', 'top', 'right']].set_visible(False)
plt.show()pres_2020 = voters.loc[2020]
pres_2016 = voters.loc[2016]
pres_2012 = voters.loc[2012]
pres_2008 = voters.loc[2008]
pres_2004 = voters.loc[2004]
pres_2000 = voters.loc[2000]
pres_1996 = voters.loc[1996]
pres_1992 = voters.loc[1992]
pres_1988 = voters.loc[1988]
pres_1984 = voters.loc[1984]
pres_1980 = voters.loc[1980]
pres_1976 = voters.loc[1976]
pres_1972 = voters.loc[1972]
pres_1968 = voters.loc[1968]
pres_1964 = voters.loc[1964]pres_2020Bar Plot
Here are four bar plots showing each age group's (and the total) voter participation percentage for each of the most recent four presidential elections.
fig, ax = plt.subplots(2, 2, sharey=True, figsize=(12,8))
bar20 = ax[0,0].bar(pres_2020.index, pres_2020['tot_part'], color='lightcoral')
ax[0,0].set_title('2020 Voter Participation % by Age')
ax[0,0].bar_label(bar20, labels=[f"{x.get_height():.1f}%" for x in bar20], label_type='center')
ax[0,0].spines[['left', 'top', 'right']].set_visible(False)
bar16 = ax[0,1].bar(pres_2016.index, pres_2016['tot_part'], color='lightcoral')
ax[0,1].set_title('2016 Voter Participation % by Age')
ax[0,1].bar_label(bar16, labels=[f"{x.get_height():.1f}%" for x in bar16], label_type='center')
ax[0,1].spines[['left', 'top', 'right']].set_visible(False)
bar12 = ax[1,0].bar(pres_2012.index, pres_2012['tot_part'], color='lightcoral')
ax[1,0].set_title('2012 Voter Participation % by Age')
ax[1,0].bar_label(bar12, labels=[f"{x.get_height():.1f}%" for x in bar12], label_type='center')
ax[1,0].spines[['left', 'top', 'right']].set_visible(False)
bar08 = ax[1,1].bar(pres_2008.index, pres_2008['tot_part'], color='lightcoral')
ax[1,1].set_title('2008 Voter Participation % by Age')
ax[1,1].bar_label(bar08, labels=[f"{x.get_height():.1f}%" for x in bar08], label_type='center')
ax[1,1].spines[['left', 'top', 'right']].set_visible(False)
plt.show()pres_2020_total = pres_2020.loc[pres_2020.index.isin(['total'])]
pres_2020_age_bins = pres_2020.loc[~pres_2020.index.isin(['total'])]
pres_2016_total = pres_2016.loc[pres_2016.index.isin(['total'])]
pres_2016_age_bins = pres_2016.loc[~pres_2016.index.isin(['total'])]
pres_2012_total = pres_2012.loc[pres_2012.index.isin(['total'])]
pres_2012_age_bins = pres_2012.loc[~pres_2012.index.isin(['total'])]
pres_2008_total = pres_2008.loc[pres_2008.index.isin(['total'])]
pres_2008_age_bins = pres_2008.loc[~pres_2008.index.isin(['total'])]
Combination Plot
Here are four plots that show the voter participation percentages by age groups as bar charts, and the total as a line plot. There are four of these plots, showing each of the most recent four presidential elections.