-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
barchart_demo.py
190 lines (155 loc) · 5.12 KB
/
barchart_demo.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- coding: utf-8 -*-
# https://matplotlib.org/3.2.1/gallery/statistics/barchart_demo.html
import sys
from collections import namedtuple
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
from matplotlib.ticker import MaxNLocator
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
fig = Figure(figsize=(9, 7))
canvas = FigureCanvas(fig)
canvas.resize(640, 480)
canvas.show()
np.random.seed(42)
Student = namedtuple("Student", ["name", "grade", "gender"])
Score = namedtuple("Score", ["score", "percentile"])
# GLOBAL CONSTANTS
test_names = ["Pacer Test", "Flexed Arm\n Hang", "Mile Run", "Agility", "Push Ups"]
test_meta = dict(zip(test_names, ["laps", "sec", "min:sec", "sec", ""]))
def attach_ordinal(num):
"""Convert an integer to an ordinal string, e.g. 2 -> '2nd'."""
suffixes = {
str(i): v
for i, v in enumerate(
["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"]
)
}
v = str(num)
# special case early teens
if v in {"11", "12", "13"}:
return v + "th"
return v + suffixes[v[-1]]
def format_score(scr, test):
"""
Build up the score labels for the right Y-axis by first
appending a carriage return to each string and then tacking on
the appropriate meta information (i.e., 'laps' vs. 'seconds'). We
want the labels centered on the ticks, so if there is no meta
info (like for pushups) then don't add the carriage return to
the string
"""
md = test_meta[test]
if md:
return "{}\n{}".format(scr, md)
else:
return scr
def format_ycursor(y):
y = int(y)
if y < 0 or y >= len(test_names):
return ""
else:
return test_names[y]
def plot_student_results(student, scores, cohort_size):
# create the figure
ax1 = fig.subplots()
fig.subplots_adjust(left=0.115, right=0.88)
fig.canvas.set_window_title("Eldorado K-8 Fitness Chart")
pos = np.arange(len(test_names))
rects = ax1.barh(
pos,
[scores[k].percentile for k in test_names],
align="center",
height=0.5,
tick_label=test_names,
)
ax1.set_title(student.name)
ax1.set_xlim([0, 100])
ax1.xaxis.set_major_locator(MaxNLocator(11))
ax1.xaxis.grid(True, linestyle="--", which="major", color="grey", alpha=0.25)
# Plot a solid vertical gridline to highlight the median position
ax1.axvline(50, color="grey", alpha=0.25)
# Set the right-hand Y-axis ticks and labels
ax2 = ax1.twinx()
scoreLabels = [format_score(scores[k].score, k) for k in test_names]
# set the tick locations
ax2.set_yticks(pos)
# make sure that the limits are set equally on both yaxis so the
# ticks line up
ax2.set_ylim(ax1.get_ylim())
# set the tick labels
ax2.set_yticklabels(scoreLabels)
ax2.set_ylabel("Test Scores")
xlabel = (
"Percentile Ranking Across {grade} Grade {gender}s\n"
"Cohort Size: {cohort_size}"
)
ax1.set_xlabel(
xlabel.format(
grade=attach_ordinal(student.grade),
gender=student.gender.title(),
cohort_size=cohort_size,
)
)
rect_labels = []
# Lastly, write in the ranking inside each bar to aid in interpretation
for rect in rects:
# Rectangle widths are already integer-valued but are floating
# type, so it helps to remove the trailing decimal point and 0 by
# converting width to int type
width = int(rect.get_width())
rankStr = attach_ordinal(width)
# The bars aren't wide enough to print the ranking inside
if width < 40:
# Shift the text to the right side of the right edge
xloc = 5
# Black against white background
clr = "black"
align = "left"
else:
# Shift the text to the left side of the right edge
xloc = -5
# White on magenta
clr = "white"
align = "right"
# Center the text vertically in the bar
yloc = rect.get_y() + rect.get_height() / 2
label = ax1.annotate(
rankStr,
xy=(width, yloc),
xytext=(xloc, 0),
textcoords="offset points",
ha=align,
va="center",
color=clr,
weight="bold",
clip_on=True,
)
rect_labels.append(label)
# make the interactive mouse over give the bar title
ax2.fmt_ydata = format_ycursor
# return all of the artists created
return {
"fig": fig,
"ax": ax1,
"ax_right": ax2,
"bars": rects,
"perc_labels": rect_labels,
}
student = Student("Johnny Doe", 2, "boy")
scores = dict(
zip(
test_names,
(
Score(v, p)
for v, p in zip(
["7", "48", "12:52", "17", "14"],
np.round(np.random.uniform(0, 100, len(test_names)), 0),
)
),
)
)
cohort_size = 62 # The number of other 2nd grade boys
arts = plot_student_results(student, scores, cohort_size)
sys.exit(app.exec_())