import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
import calendar # to convert number to month
About the Data
Note
This week we’re exploring the Long Beach Animal Shelter Data!
The dataset comes from the City of Long Beach Animal Care Services via the {animalshelter} R package.
This dataset comprises of the intake and outcome record from Long Beach Animal Shelter.
1 Initializing
1.1 Load libraries
1.2 Set theme
'~/Documents/GitHub/tidytuesday/posts/2025-03-04/rb-style.mplstyle')
plt.style.use(
# Color palette
= [
color_map "#495373",
"#ce4441",
"#ee8577",
"#eb7926",
"#ffbb44",
"#859b6c",
"#62929a",
"#004f63",
"#122451",
]
1.3 Load this week’s data
= pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-03-04/longbeach.csv') longbeach
2 Time to plot!
Plot 1
# Add year
"outcome_year"] = pd.to_datetime(longbeach["outcome_date"]).dt.year
longbeach[
# Start plot ----------------------------------------------------------------------------
= plt.subplots()
fig, ax 5)
fig.set_figwidth(3)
fig.set_figheight(
# Grid
True)
ax.set_axisbelow(True, axis="y", which="major", linestyle="-", linewidth=0.7, color="#d3daed")
ax.grid(
="outcome_year", binwidth=1, color = color_map[0],element="step")
sns.histplot(longbeach, x
# Apply the formatter to the y-axis
"{x:,.0f}"))
ax.yaxis.set_major_formatter(ticker.StrMethodFormatter(
"Entries by Year")
ax.set_title("Counts (n)")
ax.set_ylabel("")
ax.set_xlabel(
plt.tight_layout()
plt.show()
# ---------------------------------------------------------------------------------------
Plot 2
# Add year
"outcome_month"] = pd.to_datetime(longbeach["outcome_date"]).dt.month
longbeach[
= (
data2plot "outcome_year", "outcome_month"]).size().reset_index(name="n")
longbeach.groupby([
)"outcome_year"] = data2plot["outcome_year"].astype("int")
data2plot["outcome_month"] = data2plot["outcome_month"].astype("int")
data2plot[
= {i: calendar.month_abbr[i] for i in range(1, 13)}
month_map "outcome_month_abbr"] = data2plot["outcome_month"].apply(
data2plot[lambda x: month_map.get(x)
)
# Start plot ----------------------------------------------------------------------------
= plt.subplots()
fig, ax 5)
fig.set_figwidth(3)
fig.set_figheight(
# Grid
True)
ax.set_axisbelow(True, axis="y", which="major", linestyle="-", linewidth=0.7, color="#d3daed")
ax.grid(
sns.lineplot(="outcome_month_abbr", y="n", hue="outcome_year", palette=color_map
data2plot, x
)
# Apply the formatter to the y-axis
"{x:,.0f}"))
ax.yaxis.set_major_formatter(ticker.StrMethodFormatter(
# Labels
"Entries by Year")
ax.set_title("Counts (n)")
ax.set_ylabel("")
ax.set_xlabel(
# Legend
= plt.legend(
legend ="Year",
title={"weight": "bold"},
title_fontproperties="lower right",
loc
)='#495373')
plt.setp(legend.get_title(), color
# Plot & Pray
plt.tight_layout() plt.show()
Plot 3
# Start plot ----------------------------------------------------------------------------
# Geometry
= sns.relplot(
g
data2plot,="outcome_month_abbr",
x="n",
y="outcome_year",
hue=color_map,
palette=False,
legend="line",
kind="outcome_year",
col=4,
col_wrap=5,
zorder=1.51,
height=1
aspect
)
# Iterate over each subplot to customize further
for year, ax in g.axes_dict.items():
# Add the title as an annotation within the plot
0.8, 0.85, year, transform=ax.transAxes, color = '#495373', fontsize = 8)
ax.text(
# Plot every year's time series in the background
sns.lineplot(
data2plot,="outcome_month_abbr",
x="n",
y="outcome_year",
units=None,
estimator="#d3daed",
color=1,
linewidth=ax,
ax
)
# Grid
True)
ax.set_axisbelow(True, axis="y", which="major", linestyle="-", linewidth=0.7, color="#d3daed")
ax.grid(
# Apply the formatter to the y-axis
# g.yaxis.set_major_formatter(ticker.StrMethodFormatter("{x:,.0f}"))
# Reduce the frequency of the x axis ticks
4]+[ax.get_xticks()[-1]])
ax.set_xticks(ax.get_xticks()[::
# Labels
"Long Beach Animal Shelter Reports", y=.93, color = '#495373', weight = 'bold')
g.fig.suptitle(" ")
g.set_titles("Counts (n)")
g.set_ylabels(" ")
g.set_xlabels(
# Legend
# legend = g.legend(
# title="Year",
# title_fontproperties={"weight": "bold"},
# loc="lower right",
# )
# g.setp(legend.get_title(), color='#495373')
# Plot & Pray
g.tight_layout()