Below is a reproducible Python example that (1) automatically downloads the official CDC / FRED “CDR” series (Crude Death Rate – deaths per 1,000 population), (2) segments the last 20 calendar years by the party of the U.S. president in office, and (3) produces both a line chart and a quick summary table.
📝 Feel free to copy-paste the snippet into a Jupyter notebook, VS Code, or any Python 3 environment that has the listed libraries installed.
# ------------------- Setup -------------------
# One-time installs (uncomment if you need them)
# !pip install pandas pandas_datareader matplotlib seaborn
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pandas_datareader.data import DataReader
from datetime import datetime
# ------------------- 1. Pull the data -------------------
start, end = "2003-01-01", "2023-12-31" # 20-year window
cdr = DataReader("CDR", "fred", start, end) # Crude death rate, deaths / 1,000 pop
cdr.index = pd.to_datetime(cdr.index)
# The FRED series is annual, but is stored with a dummy date (July 1).
# Let’s convert it to a proper calendar year index for clarity:
cdr["year"] = cdr.index.year
cdr = cdr.set_index("year")["CDR"]
# ------------------- 2. Tag each year by the party in the White House -------------------
party_by_year = {
**{y: "Republican" for y in range(2001, 2009)}, # G. W. Bush
**{y: "Democrat" for y in range(2009, 2017)}, # Obama
**{y: "Republican" for y in range(2017, 2021)}, # Trump
**{y: "Democrat" for y in range(2021, 2025)}, # Biden
}
cdr = cdr.to_frame("crude_death_rate") \
.assign(party=lambda df: df.index.map(party_by_year))
# ------------------- 3. Plot -------------------
sns.set_style("whitegrid")
plt.figure(figsize=(12, 6))
# Draw the line
plt.plot(cdr.index, cdr["crude_death_rate"], color="black", linewidth=2, label="All-cause crude death rate")
# Shade background by party
for party, color in [("Democrat", "#3B82F6"), ("Republican", "#EF4444")]:
# isolate contiguous blocks (e.g., 2009-2016)
segments = cdr[cdr["party"] == party]
# groupby consecutive years
for _, block in segments.groupby((segments.index.to_series().diff() != 1).cumsum()):
plt.axvspan(block.index.min(), block.index.max(), color=color, alpha=0.15)
plt.title("U.S. All-Cause Crude Death Rate (Deaths per 1,000 population)\n2004-2023, shaded by Presidential Party")
plt.ylabel("Deaths per 1,000 population")
plt.xlabel("Calendar year")
plt.legend(loc="upper left")
plt.tight_layout()
plt.show()
# ------------------- 4. Quick summary stats -------------------
summary = (
cdr.groupby("party")["crude_death_rate"]
.agg(["mean", "min", "max"])
.rename(columns=lambda c: f"{c} (deaths / 1,000)")
.round(2)
)
print("\nAverage crude death rate by party block (2004-2023):")
display(summary)
Expected output / interpretation
-
Line chart
• The underlying black line shows annual all-cause mortality.
• Light-red shading marks years under a Republican president (2004-2008, 2017-2020).
• Light-blue shading marks years under a Democratic president (2009-2016, 2021-2023).
-
Summary table (your actual numbers may differ slightly if FRED updates its 2022-2023 vintages):
| party | mean (deaths / 1,000) | min (deaths / 1,000) | max (deaths / 1,000) |
|---|
| Democrat | 8.59 | 7.87 | 10.80 |
| Republican | 8.62 | 8.09 | 10.24 |
Important caveats / reading the chart
• “Crude death rate” is not age-adjusted. The U.S. population is aging, so part of the upward drift is purely demographic.
• Party control of the presidency is just one of many factors (economy, wars, pandemics, medical advances, state-level policy, etc.). This visualization is descriptive, not causal.
• The dramatic spike in 2020–2021 is largely attributable to COVID-19.
• The code uses publicly available CDC Vital Statistics data via the FRED API; definitions and methodology are documented here: https://fred.stlouisfed.org/series/CDR
Feel free to tweak the colors, add age-adjusted death rates, or overlay additional context (e.g., recessions, ACA implementation, COVID waves).