Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	File size: 1,780 Bytes
			
			| b07caec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | # ===================
# Part 1: Importing Libraries
# ===================
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
# ===================
# Part 2: Data Preparation
# ===================
year = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2018]
population_by_continent = {
    "africa": [228, 284, 365, 477, 631, 814, 1044, 1275],
    "americas": [943, 606, 540, 727, 840, 425, 519, 619],
    "asia": [1394, 1686, 2120, 1625, 1202, 1714, 2169, 2560],
    "europe": [220, 253, 276, 295, 310, 303, 294, 293],
    "oceania": [200, 300, 340, 360, 280, 260, 320, 280],
}
# Extracted variables
legend_labels = list(population_by_continent.keys())
xlim_values = (1950, 2018)
ylim_values = (0, 6000)
xlabel_value = "Year"
ylabel_value = "Number of people (millions)"
title_value = "World population"
legend_loc = "upper center"
legend_reverse = False
legend_frameon = False
legend_ncol = 5
legend_bbox_to_anchor = (0.5, 1.08)
title_y_position = 1.08
colors = ["#b2e7aa", "#fae18f", "#d75949", "#f0906d", "#a1a8d6"]
# ===================
# Part 3: Plot Configuration and Rendering
# ===================
fig, ax = plt.subplots(figsize=(8, 6))
ax.stackplot(
    year,
    population_by_continent.values(),
    labels=legend_labels,
    alpha=0.8,
    colors=colors,
)
ax.legend(
    loc=legend_loc,
    reverse=legend_reverse,
    frameon=legend_frameon,
    ncol=legend_ncol,
    bbox_to_anchor=legend_bbox_to_anchor,
)
ax.set_xlim(*xlim_values)
ax.set_ylim(*ylim_values)
ax.set_title(title_value, y=title_y_position)
ax.set_xlabel(xlabel_value)
ax.set_ylabel(ylabel_value)
ax.tick_params(axis="both", which="both", length=0)
# ===================
# Part 4: Saving Output
# ===================
plt.tight_layout()
plt.savefig("area_5.pdf", bbox_inches="tight")
 | 
