-
Notifications
You must be signed in to change notification settings - Fork 0
/
histogram.py
41 lines (33 loc) · 1.06 KB
/
histogram.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
import sys
import matplotlib.pyplot as plt
from preprocessing import load_data
from utils import houses, get_house_color
def to_histogram(df, feature: str, ax: plt.axes):
""" Plots a histogram for the grade repartition of a feature,
separated by house.
"""
for house in houses:
ax.hist(
df.loc[df['Hogwarts House'] == house, feature],
alpha=.5,
label=house,
color=get_house_color(house)
)
def main():
try:
if len(sys.argv) != 2:
raise IndexError('Please enter one argument.')
df, features = load_data(sys.argv[1])
features = df.select_dtypes('number').columns.tolist()
for feature in features:
_, ax = plt.subplots()
to_histogram(df, feature, ax)
plt.title(feature)
plt.xlabel('Grade')
plt.ylabel('Number of students')
plt.legend(title='House')
plt.show()
except BaseException as e:
print(type(e).__name__, ':', e)
if __name__ == '__main__':
main()