Code
import pandas as pd
import plotly.express as px
# Load the demographic share dataset
= pd.read_csv("home_ownership_generation.csv", sep=",")
df_ownership
# Function to plot home ownership rates by age and generation using Plotly
def plot_ownership_by_age_and_generation_plotly(df):
# Define the custom color palette
= {
colors "Gen X": "#636EFA", # blue
"Baby Boomer": "#B6E880", # light green
"Gen Z": "#FFA15A", # orange
"Millennial": "#EF553B", # red
"Silent": "#FF97FF", # pink
}
# Create the plot
= px.line(
fig
df,="Age group",
x="Home Ownership Rate",
y="Generation",
color="",
title={
labels"Age group": "Age Group",
"Home Ownership Rate": "Home Ownership Rate (%)",
},="linear",
line_shape=colors, # Apply the custom color palette
color_discrete_map
)
fig.update_layout(="Age Group",
xaxis_title="",
yaxis_title="",
legend_title_text="plotly_dark",
template=dict(tickangle=45),
xaxis="#282a36",
plot_bgcolor="#282a36",
paper_bgcolor=dict(size=14, family="Consolas"), # Set the font to Consolas
font=dict(size=18, family="Consolas"),
title_font=500, # Adjust based on your needs
width=450, # Adjust based on your needs
height=dict(l=20, r=20, t=40, b=20),
margin
)
="", range=[40, 90])
fig.update_yaxes(title_text
fig.show()
# Plot the data using Plotly
= (
df_ownership_grouped
df_ownership["Generation"] != "Other")
(df_ownership[& (df_ownership["Generation"] != "Total")
& (~df_ownership["Age group"].isin(["0-4", "5-9", "10-14"]))
]"Age group", "Generation"])["Home Ownership Rate"]
.groupby([
.mean()
.reset_index()
)
plot_ownership_by_age_and_generation_plotly(df_ownership_grouped)
Source: Statistics NZ