-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_graph.py
More file actions
46 lines (35 loc) · 1.54 KB
/
Copy pathcreate_graph.py
File metadata and controls
46 lines (35 loc) · 1.54 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
import csv
import matplotlib.pyplot as plt
class CreateGraph:
def __init__(self):
print("Initializing CreateGraph object...")
def generate_population_dictionary(self, file_name='data.csv'):
population_per_continent = {
} #init empty dictionary to populate with data read from csv
file_name = 'data.csv'
with open(file_name, mode='r') as file:
csvFile = csv.DictReader(file)
for lines in csvFile:
continent = lines['continent']
year = int(lines['year'])
population = int(lines['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 display_graph(self, population_per_continent):
for continent in population_per_continent:
population = population_per_continent[continent][
'population'] # loops through main_dict[continent key]['population' key]
years = population_per_continent[continent][
'years'] #loops through main_dict[continent key]['years' key]
plt.plot(years, population, label=continent, marker=".", alpha=0.5)
plt.title("Internet Population (per continent)", fontsize=12)
plt.suptitle("VizPy - Data visualizer", fontsize=16)
plt.xlabel("Year")
plt.ylabel("Internet users (in millions)")
plt.grid(True)
plt.tight_layout()
plt.legend()
plt.show()