-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbirth_rate.py
More file actions
39 lines (33 loc) · 1.26 KB
/
birth_rate.py
File metadata and controls
39 lines (33 loc) · 1.26 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
"""this script visualizes the birth rate of various countries"""
import csv
import pygal_maps_world.maps
from country_codes import get_country_code
# Dictionary to store birth rates with country codes as keys
birth_rate = {}
# setting filename
FILENAME = "Global_Education.csv"
# reading the file
with open(FILENAME, "r", encoding="latin-1") as file_obj:
reader = csv.reader(file_obj)
next(reader)
for row in reader:
country_name = row[0]
# deriving the country code of the countries
code = get_country_code(country_name)
if code:
try:
birth_rate[code] = float(row[25])
except (ValueError, IndexError):
print("missing data")
else:
print(f"the country code can not be generated for {country_name}")
# creating a function for the visualization
def create_world_map(title, data, filename):
"""Create a world map visualization with given title, data, and filename."""
wm = pygal_maps_world.maps.World()
wm.title = title
wm.add("birthrate", data)
wm.render_to_file(filename)
# creating an instance of the function
create_world_map("The Birth Rate of Male and Female ", birth_rate, "birth_rate.svg")
print("The worldmap has been succesfully visualized")