-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Advanced_Project.py
More file actions
57 lines (44 loc) · 1.82 KB
/
Python_Advanced_Project.py
File metadata and controls
57 lines (44 loc) · 1.82 KB
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
import csv
import matplotlib.pyplot as plt
def generate_population_dictionary(filename):
"""Generates a dictionary with continents as keys and their respective populations and years as values.
{
"Africa": {
"population": [1.2, 1.5, 1.8],
"years": [2000, 2005, 2010]
},
"Asia": {
"population": [3.0, 3.5, 4.0],
"years": [2000, 2005, 2010]
}
}
"""
population_per_continent = {}
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for line in reader:
continent = line['continent']
year = line['year']
population = int(line['population'])
if continent not in population_per_continent:
population_per_continent[continent] = {'population':[], 'years': []}
population_per_continent[continent]['population'].append(population)
population_per_continent[continent]['years'].append(year)
return population_per_continent
def generate_plot_from_dictionary(population_dictionary):
"""Generates a plot from the population dictionary for each continent."""
for continent in population_dictionary:
years = population_dictionary[continent]['years']
populations = population_dictionary[continent]['population']
plt.plot(years, populations, label=continent, marker='o', alpha=0.7)
plt.title(' Internet Population Growth by Continent')
plt.xlabel('Year')
plt.ylabel('Internet Users (in billions)')
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()
filename = 'data.csv'
"""Generates a population dictionary and a plot to display the internet population growth by continent."""
population_per_continent = generate_population_dictionary(filename)
generate_plot_from_dictionary(population_per_continent)