From 201512ef3a60b4450dcb51d73ab99d62c162907a Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Sat, 16 Dec 2023 22:35:28 +0000 Subject: [PATCH] fix range and --- python/population_plot.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/python/population_plot.py b/python/population_plot.py index 482514d..c101633 100644 --- a/python/population_plot.py +++ b/python/population_plot.py @@ -1,7 +1,7 @@ import matplotlib.pyplot as plt import numpy as np -# Initial population at year 1 +# Initial population at year 0 initial_population = 1_000_000 death_rate = 0.10 @@ -10,30 +10,30 @@ start_year = 4000 stop_year = 10000 interval = 500 # Interval for plotting data points -# Calculate the population at the start of the start_year +# Initialize population calculations current_population = initial_population -for _ in range(start_year // 100): # Dividing by 100 as the death rate is applied every 100 years - current_population -= death_rate * current_population +population_values = [initial_population] # Include initial population as the first data point +years = np.arange(0, stop_year + 1, 100) # Calculate every 100 years -# Now calculate and plot the population from start_year to stop_year -years = np.arange(start_year, stop_year + 1, interval) -population_values = [] -for year in years: - # Apply the death rate for each interval (interval/100 times for each period) - for _ in range(interval // 100): - current_population -= death_rate * current_population +# Apply the death rate for each 100-year period +for year in years[1:]: + current_population -= death_rate * current_population population_values.append(current_population) +# Filter the years and population values for the graph +graph_years = np.arange(start_year, stop_year + 1, interval) +graph_population_values = [population for year, population in zip(years, population_values) if year >= start_year and year in graph_years] + # Create the graph with labels for each data point plt.figure(figsize=(8, 6)) -plt.plot(years, population_values, marker='o', linestyle='-', color='b') +plt.plot(graph_years, graph_population_values, marker='o', linestyle='-', color='b') plt.xlabel('Years') plt.ylabel('Population') plt.title(f'Population Projection from Year {start_year} to {stop_year}') # Adding labels to the data points -for i, txt in enumerate(population_values): - plt.annotate(f"{int(txt):,}", (years[i], population_values[i]), textcoords="offset points", xytext=(0,10), ha='center') +for i, txt in enumerate(graph_population_values): + plt.annotate(f"{int(txt):,}", (graph_years[i], graph_population_values[i]), textcoords="offset points", xytext=(0,10), ha='center') plt.grid(True) plt.show()