• Source
    1. import pandas as pd
    2. import matplotlib.pyplot as plt
    3.  
    4. # Data input
    5. hic_data = {
    6. "Country": ["Spain", "Norway", "Luxembourg", "Switzerland", "Ireland"],
    7. "Mean Age at First Birth": [31.5, 30.1, 31.4, 31.3, 31.6]
    8. }
    9.  
    10. lic_mic_data = {
    11. "Country": ["Rwanda", "Algeria", "Ukraine", "Philippines", "Iran"],
    12. "Mean Age at First Birth": [22.5, 24.5, 24.9, 22.3, 27.4]
    13. }
    14.  
    15. # Convert to DataFrames
    16. hic_df = pd.DataFrame(hic_data)
    17. lic_mic_df = pd.DataFrame(lic_mic_data)
    18.  
    19. # Calculate overall means
    20. hic_mean = hic_df["Mean Age at First Birth"].mean()
    21. lic_mic_mean = lic_mic_df["Mean Age at First Birth"].mean()
    22.  
    23. # Combine for display
    24. summary_df = pd.DataFrame({
    25. "Group": ["High-Income Countries (HICs)", "Low- and Middle-Income Countries (LICs/MICs)"],
    26. "Overall Mean Age at First Birth": [hic_mean, lic_mic_mean]
    27. })
    28.  
    29. # Plot chart
    30. plt.figure(figsize=(10,6))
    31. plt.bar(hic_df["Country"], hic_df["Mean Age at First Birth"], label="HICs", alpha=0.7)
    32. plt.bar(lic_mic_df["Country"], lic_mic_df["Mean Age at First Birth"], label="LICs/MICs", alpha=0.7)
    33. plt.axhline(hic_mean, color='blue', linestyle='--', linewidth=1.2, label=f"HIC Mean: {hic_mean:.1f}")
    34. plt.axhline(lic_mic_mean, color='orange', linestyle='--', linewidth=1.2, label=f"LIC/MIC Mean: {lic_mic_mean:.1f}")
    35. plt.title("Mean Age at First Birth by Country", fontsize=14)
    36. plt.ylabel("Mean Age at First Birth (years)", fontsize=12)
    37. plt.xticks(rotation=45)
    38. plt.legend()
    39. plt.tight_layout()
    40.  
    41. import caas_jupyter_tools
    42. caas_jupyter_tools.display_dataframe_to_user("Summary of Overall Mean Ages", summary_df)
    43.  
    44. plt.show()
    45.