-
Notifications
You must be signed in to change notification settings - Fork 0
/
36_birthday_plots.py
79 lines (62 loc) · 2.57 KB
/
36_birthday_plots.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
# Exercise: https://www.practicepython.org/exercise/2017/04/02/36-birthday-plots.html
# This exercise is Part 4 of 4 of the birthday data exercise series. The other exercises are: Part 1, Part 2, and Part 3.
# In the previous exercise we counted how many birthdays there are in each month in our dictionary of birthdays.
# In this exercise, use the bokeh Python library to plot a histogram of which months the scientists have birthdays in!
# Because it would take a long time for you to input the months of various scientists, you can use my scientist birthday
# JSON file. Just parse out the months (if you don’t know how, I suggest looking at the previous exercise or its solution)
# and draw your histogram.
# If you are using a purely web-based interface for coding, this exercise won’t work for you,
# since it requires installing the bokeh Python package. Now might be a good time to install Python on your own computer.
from bokeh.plotting import figure, show, output_file
from collections import Counter as c
import json
fileName = "34_birthdays.json"
def get_birthdays():
global birthdays
with open(fileName, "r") as f:
birthdays = json.load(f)
return birthdays
def get_months_counter():
months = list()
birthdays = get_birthdays()
for value in birthdays.values():
month_number = value[:2]
if month_number == "01":
months.append("Jan")
elif month_number == "02":
months.append("Feb")
elif month_number == "03":
months.append("Mar")
elif month_number == "04":
months.append("Apr")
elif month_number == "05":
months.append("May")
elif month_number == "06":
months.append("Jun")
elif month_number == "07":
months.append("Jul")
elif month_number == "08":
months.append("Aug")
elif month_number == "09":
months.append("Sep")
elif month_number == "10":
months.append("Oct")
elif month_number == "11":
months.append("Nov")
elif month_number == "12":
months.append("Dec")
result = c(months)
return result
def show_chart(x, y):
output_file("36_plot.html")
x_categories = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dic"]
p = figure(x_range=x_categories)
p.vbar(x=x, top=y, width=0.5)
show(p)
def run():
result = get_months_counter()
x = list(result.keys())
y = list(result.values())
show_chart(x, y)
if __name__ == "__main__":
run()