-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchart.py
62 lines (40 loc) · 1.56 KB
/
chart.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
import numpy as np
from matplotlib import pyplot as plt
def average_weight_for_attribute(dataframe, attrname, tuple=None):
attribute = dataframe[dataframe['attribute'] == attrname]
if tuple is not None:
attribute = attribute[attribute['tuple'] == tuple]
return attribute['weight'].mean(), attribute['weight'].std()
def count_explation_with_attr(dataframe, attrname, tuple=None):
attribute = dataframe[dataframe['attribute'] == attrname]
if tuple is not None:
attribute = attribute[attribute['tuple'] == tuple]
return len(attribute['exp'].unique())
def count_explanation(dataframe):
return len(dataframe['exp'].unique())
def chart(data, subplot, ylim, title="", w=True):
bars = []
spacing = 2
N = np.ones((1,))
ticks = []
plt.subplot(*subplot)
plt.grid(True)
plt.ylim(ylim)
attr_sorted = sorted(data['attribute'].unique(), reverse=True)
for j, att in enumerate(attr_sorted, start=1):
att_m, att_s = average_weight_for_attribute(data, att, None)
count = count_explation_with_attr(data, att)
alpha = count / count_explanation(data)
color = (0.8, 0.1, 0, 1) if att_m < 0 else (0, 0.8, 0.1, 1)
xs = ((N+spacing) * j) + np.arange(1)
ticks.append(xs[0])
alpha = alpha * 2.5 if w else 2
plt.bar(xs,
[att_m],
color=[color],
yerr=[att_s],
width=2,
align='center')
plt.title(title)
plt.ylabel("Score")
plt.xticks(ticks, attr_sorted, rotation=90)