0% found this document useful (0 votes)
8 views1 page

Choropleth Mapping Population Density With Log and Exp - Py

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Choropleth Mapping Population Density With Log and Exp - Py

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1 import numpy as np

2 import matplotlib.pyplot as plt


3 import geopandas as gpd
4 import pandas as pd
5 import os
6
7 # Parameters for the growth models
8 number_of_deaths = 3683
9 total_population = sum([25228, 11274, 25964, 12323, 97879, 32174, 23367, 60607, 11844, 36621, 29390, 14690, 14234,
10 41415, 34034, 35532, 10949, 8882, 27867, 17641, 29882, 6928, 15100, 15361]) # Total population
11 death_rate = (number_of_deaths / total_population) * 1000 # Death rate per 1,000 people
12 d = death_rate / 1000 # Convert death rate back to a proportion
13
14 # Municipalities data
15 merged_data = [
16 {'Municipality': 'Allen', 'Population (2020)': 25228, 'Annual Growth Rate': -0.002, 'Area': 47.60},
17 {'Municipality': 'Biri', 'Population (2020)': 11274, 'Annual Growth Rate': -0.009, 'Area': 24.62},
18 {'Municipality': 'Bobon', 'Population (2020)': 25964, 'Annual Growth Rate': 0.0197, 'Area': 130.00},
19 {'Municipality': 'Capul', 'Population (2020)': 12323, 'Annual Growth Rate': -0.006, 'Area': 35.56},
20 {'Municipality': 'Catarman', 'Population (2020)': 97879, 'Annual Growth Rate': 0.0085, 'Area': 464.43},
21 {'Municipality': 'Catubig', 'Population (2020)': 32174, 'Annual Growth Rate': -0.0055, 'Area': 217.02},
22 {'Municipality': 'Gamay', 'Population (2020)': 23367, 'Annual Growth Rate': -0.0013, 'Area': 115.10},
23 {'Municipality': 'Laoang', 'Population (2020)': 60607, 'Annual Growth Rate': -0.0026, 'Area': 246.94},
24 {'Municipality': 'Lapinig', 'Population (2020)': 11844, 'Annual Growth Rate': -0.0197, 'Area': 57.30},
25 {'Municipality': 'Las Navas', 'Population (2020)': 36621, 'Annual Growth Rate': -0.0075, 'Area': 282.61},
26 {'Municipality': 'Lavezares', 'Population (2020)': 29390, 'Annual Growth Rate': 0.0045, 'Area': 119.50},
27 {'Municipality': 'Lope de Vega', 'Population (2020)': 14690, 'Annual Growth Rate': 0.0, 'Area': 280.00},
28 {'Municipality': 'Mapanas', 'Population (2020)': 14234, 'Annual Growth Rate': 0.0031, 'Area': 117.85},
29 {'Municipality': 'Mondragon', 'Population (2020)': 41415, 'Annual Growth Rate': 0.0142, 'Area': 288.90},
30 {'Municipality': 'Palapag', 'Population (2020)': 34034, 'Annual Growth Rate': -0.0016, 'Area': 179.60},
31 {'Municipality': 'Pambujan', 'Population (2020)': 35532, 'Annual Growth Rate': 0.0153, 'Area': 163.90},
32 {'Municipality': 'Rosario', 'Population (2020)': 10949, 'Annual Growth Rate': 0.0085, 'Area': 31.60},
33 {'Municipality': 'San Antonio', 'Population (2020)': 8882, 'Annual Growth Rate': -0.0041, 'Area': 27.00},
34 {'Municipality': 'San Isidro', 'Population (2020)': 27867, 'Annual Growth Rate': 0.0094, 'Area': 255.90},
35 {'Municipality': 'San Jose', 'Population (2020)': 17641, 'Annual Growth Rate': 0.001, 'Area': 29.85},
36 {'Municipality': 'San Roque', 'Population (2020)': 29882, 'Annual Growth Rate': -0.0048, 'Area': 152.98},
37 {'Municipality': 'San Vicente', 'Population (2020)': 6928, 'Annual Growth Rate': -0.0261, 'Area': 15.80},
38 {'Municipality': 'Silvino Lobos', 'Population (2020)': 15100, 'Annual Growth Rate': -0.0028, 'Area': 224.20},
39 {'Municipality': 'Victoria', 'Population (2020)': 15361, 'Annual Growth Rate': 0.0076, 'Area': 186.70}
40 ]
41
42 df = pd.DataFrame(merged_data)
43
44 # Load shapefile
45 gdf = gpd.read_file('C:/Users/DWYNE S MARQUITA/OneDrive/Desktop/Graphing and Stuff/choropleth/gadm41_PHL_shp/gadm41_PHL_2.shp')
46
47 # Merge the geodataframe with the DataFrame
48 merged_gdf = gdf.merge(df, left_on='NAME_2', right_on='Municipality')
49
50 # Functions for logistic and exponential growth models
51 def exponential_growth(P0, r, t):
52 return P0 * np.exp(r * (t - 2020))
53
54 def logistic_growth(P0, r, K, t):
55 return K / (1 + (K / P0 - 1) * np.exp(-r * (t - 2020)))
56
57 # Create output directories for logistic and exponential growth
58 output_dir_logistic = 'C:/Users/DWYNE S MARQUITA/OneDrive/Desktop/Graphing and Stuff/choropleth/Logistic_Growth'
59 output_dir_exponential = 'C:/Users/DWYNE S MARQUITA/OneDrive/Desktop/Graphing and Stuff/choropleth/Exponential_Growth'
60 os.makedirs(output_dir_logistic, exist_ok=True)
61 os.makedirs(output_dir_exponential, exist_ok=True)
62
63 # Time steps from 2020 to 2120 (100 years)
64 years = np.linspace(2020, 2050, 31)
65
66 # Generate maps for each year using both growth models
67 for year in range(0, 31):
68 current_year = 2020 + year
69
70 # Calculate the carrying capacity for each municipality using logistic growth
71 merged_gdf['Carrying Capacity'] = (merged_gdf['Annual Growth Rate'] * merged_gdf['Population (2020)']) / d
72
73 # Calculate exponential and logistic population projections
74 merged_gdf['Exponential Population'] = merged_gdf.apply(
75 lambda row: exponential_growth(row['Population (2020)'], row['Annual Growth Rate'], current_year), axis=1)
76
77 merged_gdf['Logistic Population'] = merged_gdf.apply(
78 lambda row: logistic_growth(row['Population (2020)'], row['Annual Growth Rate'], row['Carrying Capacity'], current_year), axis=1)
79
80 # Calculate population density
81 merged_gdf['Exponential Population Density'] = merged_gdf['Exponential Population'] / merged_gdf['Area']
82 merged_gdf['Logistic Population Density'] = merged_gdf['Logistic Population'] / merged_gdf['Area']
83
84 # Cap the population density at 500 for visualization purposes
85 merged_gdf['Exponential Population Density'] = merged_gdf['Exponential Population Density'].clip(upper=500)
86 merged_gdf['Logistic Population Density'] = merged_gdf['Logistic Population Density'].clip(upper=500)
87
88 # Plot and save maps for exponential growth model
89 fig, ax = plt.subplots(1, 1, figsize=(10, 6))
90 ax.set_xlim([124, 125.5])
91 ax.set_ylim([12.2, 12.8])
92 merged_gdf.plot(column='Exponential Population Density', cmap='OrRd', legend=True, ax=ax,
93 legend_kwds={'label': "Population Density", 'orientation': "horizontal", 'shrink': 0.8},
94 vmin=0, vmax=500) # Set vmin and vmax for consistent legend
95 plt.title(f'Exponential Population Density for {current_year}', fontsize=14)
96
97 # Annotate the provinces with their names
98 for x, y, label in zip(merged_gdf.geometry.centroid.x, merged_gdf.geometry.centroid.y, merged_gdf['Municipality']):
99 ax.annotate(label, xy=(x, y), horizontalalignment='center', fontsize=8, color='black')
100
101 output_filename_exp = f'{output_dir_exponential}/exp_population_density_{current_year}.png'
102 plt.savefig(output_filename_exp)
103 plt.close()
104
105 # Plot and save maps for logistic growth model
106 fig, ax = plt.subplots(1, 1, figsize=(10, 6))
107 ax.set_xlim([124, 125.5])
108 ax.set_ylim([12.2, 12.8])
109 merged_gdf.plot(column='Logistic Population Density', cmap='Blues', legend=True, ax=ax,
110 legend_kwds={'label': "Population Density", 'orientation': "horizontal", 'shrink': 0.8},
111 vmin=0, vmax=500) # Set vmin and vmax for consistent legend
112 plt.title(f'Logistic Population Density for {current_year}', fontsize=14)
113
114 # Annotate the provinces with their names
115 for x, y, label in zip(merged_gdf.geometry.centroid.x, merged_gdf.geometry.centroid.y, merged_gdf['Municipality']):
116 ax.annotate(label, xy=(x, y), horizontalalignment='center', fontsize=8, color='black')
117
118 output_filename_log = f'{output_dir_logistic}/log_population_density_{current_year}.png'
119 plt.savefig(output_filename_log)
120 plt.close()
121

You might also like