-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathev_counts.py
More file actions
59 lines (46 loc) · 1.63 KB
/
ev_counts.py
File metadata and controls
59 lines (46 loc) · 1.63 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
54
55
56
57
58
59
import csv
filename = 'henkiloautot-sahko-merkit.csv'
csv_file = open(filename)
reader = csv.reader(csv_file)
headers = next(reader)
rows = []
for row in reader:
rows.append(row)
csv_file.close()
years = range(2016, 2026)
counts = {} # make empty dictionary for yearly counts
for row in rows:
if row[1] == '':
continue
# registration date is DD.MM.YYYY
year = int(row[1][6:])
if year not in years:
continue
if year not in counts:
counts[year] = 0
counts[year] += 1
print(f'Sähköautojen ensirekisteröinnit {years.start} - {years.stop - 1}')
for year in years:
print(f'{year}: {counts[year]:>5}')
print(f'Yhteensä: {len(rows)}')
# Virtuaaliympäristöt, kts. https://realpython.com/python-virtual-environments-a-primer/
# Tee ennen tätä virtuaaliympäristö samaan hakemistoon
# missä projekti on: `python3 -m venv venv`.
# Aktivoi virtuaaliympäristö: `source venv/bin/activate`
# Asenna sitten Matplotlib: `pip install matplotlib`.
# pip-ohjelman käyttö: kts. https://realpython.com/what-is-pip/
# Kun olet lopettanut projektin työstämisen,
# anna komento `deactivate`.
# Kaavioiden tekeminen Matplotlib-kirjastolla,
# kts. https://realpython.com/python-matplotlib-guide/
import matplotlib.pyplot as plt
year_keys = sorted(list(counts.keys()))
#print(year_keys)
values = [counts[k] for k in year_keys]
#print(values)
plt.bar(year_keys, values, tick_label=year_keys)
plt.show()
# HUOM.! Jos saat ilmoituksen "UserWarning: Matplotlib is
# currently using agg, which is a non-GUI backend, so
# cannot show the figure.", niin ratkaisu on
# asentaa Tkinter: `sudo apt-get install python3-tk`