# -*- coding: utf-8 -*- """ Created on Sun Jul 26 18:59:05 2026 @author: muhuri """ import matplotlib.pyplot as plt from pathlib import Path #---------------------------------------------------------- # Display options #---------------------------------------------------------- show_title = True #---------------------------------------------------------- # Output folder #---------------------------------------------------------- output_dir = Path(r"C:\Explore\Python_Code_BangIndia\charts") #---------------------------------------------------------- # Publication style #---------------------------------------------------------- plt.rcParams.update({ "figure.figsize": (12, 7), "figure.dpi": 150, "savefig.dpi": 600, "font.family": "DejaVu Sans", "font.size": 14, "axes.titlesize": 20, "axes.titleweight": "bold", "axes.labelsize": 16, "xtick.labelsize": 13, "ytick.labelsize": 13, "legend.fontsize": 12 }) #---------------------------------------------------------- # Census data #---------------------------------------------------------- years = [1951,1961,1974,1981,1991,2001,2011,2022] total = [42.0,50.8,76.4,87.1,111.5,129.2,149.8,165.2] muslim = [32.3,40.84,65.25,75.43,98.45,115.76,135.42,150.4] hindu = [9.24,9.40,10.31,10.54,11.71,11.89,12.73,13.13] buddhist = [0.29,0.36,0.46,0.52,0.67,0.90,0.90,1.01] christian = [0.13,0.15,0.23,0.26,0.33,0.39,0.45,0.50] others = [0.04,0.05,0.15,0.35,0.33,0.26,0.30,0.17] #---------------------------------------------------------- # Combine Buddhist, Christian, and Others #---------------------------------------------------------- others_combined = [ b + c + o for b, c, o in zip( buddhist, christian, others ) ] #---------------------------------------------------------- # Summary table #---------------------------------------------------------- summary = ( "Population (millions)\n" "Year Muslim Hindu Others* Total\n" f"1951 {muslim[0]:4.1f} {hindu[0]:4.1f} {others_combined[0]:4.1f} {total[0]:4.1f}\n" f"2022 {muslim[-1]:5.1f} {hindu[-1]:4.1f} {others_combined[-1]:4.1f} {total[-1]:5.1f}" ) plt.figtext( 0.55, 0.065, summary, ha="left", va="bottom", fontsize=10, family="monospace" ) #---------------------------------------------------------- # Create figure #---------------------------------------------------------- fig, ax = plt.subplots(figsize=(12, 7)) fig.subplots_adjust(bottom=0.28) #---------------------------------------------------------- # Stacked area chart #---------------------------------------------------------- religion_colors = { "Muslim": "#4E79A7", # Blue "Hindu": "#F28E2B", # Orange "Others": "#9AA66D" # Soft olive } ax.stackplot( years, muslim, hindu, others_combined, labels=[ "Muslim", "Hindu", "Others" ], colors=[ religion_colors["Muslim"], religion_colors["Hindu"], religion_colors["Others"] ], alpha=1.0, baseline="zero" ) #---------------------------------------------------------- # Draw boundary lines between stacked areas #---------------------------------------------------------- cum1 = muslim cum2 = [ m + h for m, h in zip( muslim, hindu ) ] cum3 = total # Draw boundaries boundary_color = "#666666" ax.plot(years, cum1, color=boundary_color, linewidth=1.2) ax.plot(years, cum2, color=boundary_color, linewidth=1.0) ax.plot(years, cum3, color=boundary_color, linewidth=1.2) #---------------------------------------------------------- # Titles #---------------------------------------------------------- if show_title: ax.set_title( "Population by Religion in Bangladesh: 1951–2022 Censuses\n" "Population Counts (Millions)", fontsize=18, fontweight="bold" ) ax.set_xlabel("Census Year") ax.set_ylabel("Population (Millions)") #---------------------------------------------------------- # Axes #---------------------------------------------------------- ax.set_xticks(years) ax.set_xlim(1951,2022) ax.set_ylim(0,170) ax.grid( linestyle="--", linewidth=0.5, alpha=0.25 ) #---------------------------------------------------------- # Legend #---------------------------------------------------------- ax.legend( loc="upper left", bbox_to_anchor=(1.01, 1), frameon=False ) #---------------------------------------------------------- # Footnote #---------------------------------------------------------- left = fig.subplotpars.left plt.figtext( left, 0.010, "Source: Bangladesh Population Censuses, 1951–2022.\n" "Note: The 'Others' category combines the Buddhist, Christian, and Other religion categories.", ha="left", va="bottom", fontsize=10 ) #---------------------------------------------------------- # Summary table #---------------------------------------------------------- table_data = [ ["1951", f"{muslim[0]:.1f}", f"{hindu[0]:.1f}", f"{others_combined[0]:.1f}", f"{total[0]:.1f}"], ["2022", f"{muslim[-1]:.1f}", f"{hindu[-1]:.1f}", f"{others_combined[-1]:.1f}", f"{total[-1]:.1f}"] ] #---------------------------------------------------------- # Summary table title #---------------------------------------------------------- plt.figtext( 0.50, 0.165, "Population (Millions)", ha="center", va="bottom", fontsize=10.5 ) table = plt.table( cellText=table_data, colLabels=[ "Year", "Muslim", "Hindu", "Others", "Total" ], cellLoc="center", colLoc="center", loc="bottom", bbox=[0.15, -0.32, 0.70, 0.12] ) table.auto_set_font_size(False) table.set_fontsize(10) # Column widths col_widths = [0.10, 0.15, 0.15, 0.15, 0.15] for col, width in enumerate(col_widths): for row in range(len(table_data) + 1): table[(row, col)].set_width(width) #---------------------------------------------------------- # Format table #---------------------------------------------------------- for (row, col), cell in table.get_celld().items(): cell.set_linewidth(0.3) if row == 0: cell.set_facecolor("#F5F5F5") cell.set_text_props( ha="center", va="center" ) #---------------------------------------------------------- # Save #---------------------------------------------------------- plt.savefig( output_dir / "BangladeshPopulationByReligion_StackedArea.png", dpi=600, bbox_inches="tight" ) plt.savefig( output_dir / "BangladeshPopulationByReligion_StackedArea.pdf", bbox_inches="tight" ) plt.show() plt.close(fig)