import pandas as pd
import matplotlib.pyplot as plt
# Data input
hic_data = {
"Country": ["Spain", "Norway", "Luxembourg", "Switzerland", "Ireland"],
"Mean Age at First Birth": [31.5, 30.1, 31.4, 31.3, 31.6]
}
lic_mic_data = {
"Country": ["Rwanda", "Algeria", "Ukraine", "Philippines", "Iran"],
"Mean Age at First Birth": [22.5, 24.5, 24.9, 22.3, 27.4]
}
# Convert to DataFrames
hic_df = pd.DataFrame(hic_data)
lic_mic_df = pd.DataFrame(lic_mic_data)
# Calculate overall means
hic_mean = hic_df["Mean Age at First Birth"].mean()
lic_mic_mean = lic_mic_df["Mean Age at First Birth"].mean()
# Combine for display
summary_df = pd.DataFrame({
"Group": ["High-Income Countries (HICs)", "Low- and Middle-Income Countries (LICs/MICs)"],
"Overall Mean Age at First Birth": [hic_mean, lic_mic_mean]
})
# Plot chart
plt.figure(figsize=(10,6))
plt.bar(hic_df["Country"], hic_df["Mean Age at First Birth"], label="HICs", alpha=0.7)
plt.bar(lic_mic_df["Country"], lic_mic_df["Mean Age at First Birth"], label="LICs/MICs", alpha=0.7)
plt.axhline(hic_mean, color='blue', linestyle='--', linewidth=1.2, label=f"HIC Mean: {hic_mean:.1f}")
plt.axhline(lic_mic_mean, color='orange', linestyle='--', linewidth=1.2, label=f"LIC/MIC Mean: {lic_mic_mean:.1f}")
plt.title("Mean Age at First Birth by Country", fontsize=14)
plt.ylabel("Mean Age at First Birth (years)", fontsize=12)
plt.xticks(rotation=45)
plt.legend()
plt.tight_layout()
import caas_jupyter_tools
caas_jupyter_tools.display_dataframe_to_user("Summary of Overall Mean Ages", summary_df)
plt.show()