29
29
# GLOBAL CONSTANTS
30
30
test_names = ['Pacer Test' , 'Flexed Arm\n Hang' , 'Mile Run' , 'Agility' ,
31
31
'Push Ups' ]
32
- test_meta = dict (zip (test_names , ['laps' , 'sec' , 'min:sec' , 'sec' , '' ]))
32
+ test_units = dict (zip (test_names , ['laps' , 'sec' , 'min:sec' , 'sec' , '' ]))
33
33
34
34
35
35
def attach_ordinal (num ):
36
36
"""Convert an integer to an ordinal string, e.g. 2 -> '2nd'."""
37
37
suffixes = {str (i ): v
38
38
for i , v in enumerate (['th' , 'st' , 'nd' , 'rd' , 'th' ,
39
39
'th' , 'th' , 'th' , 'th' , 'th' ])}
40
-
41
40
v = str (num )
42
41
# special case early teens
43
42
if v in {'11' , '12' , '13' }:
44
43
return v + 'th'
45
44
return v + suffixes [v [- 1 ]]
46
45
47
46
48
- def format_score (scr , test ):
47
+ def format_score (score , test ):
49
48
"""
50
- Build up the score labels for the right Y-axis by first
51
- appending a carriage return to each string and then tacking on
52
- the appropriate meta information (i.e., 'laps' vs. 'seconds'). We
53
- want the labels centered on the ticks, so if there is no meta
54
- info (like for pushups) then don't add the carriage return to
55
- the string
49
+ Create score labels for the right y-axis as the test name followed by the
50
+ measurement unit (if any), split over two lines.
56
51
"""
57
- md = test_meta [test ]
58
- if md :
59
- return '{0 }\n {1}' . format ( scr , md )
60
- else :
61
- return scr
52
+ unit = test_units [test ]
53
+ if unit :
54
+ return f' { score } \n { unit } '
55
+ else : # If no unit, don't include a newline, so that label stays centered.
56
+ return score
62
57
63
58
64
59
def format_ycursor (y ):
@@ -70,8 +65,7 @@ def format_ycursor(y):
70
65
71
66
72
67
def plot_student_results (student , scores , cohort_size ):
73
- # create the figure
74
- fig , ax1 = plt .subplots (figsize = (9 , 7 ))
68
+ fig , ax1 = plt .subplots (figsize = (9 , 7 )) # Create the figure
75
69
fig .subplots_adjust (left = 0.115 , right = 0.88 )
76
70
fig .canvas .set_window_title ('Eldorado K-8 Fitness Chart' )
77
71
@@ -95,16 +89,13 @@ def plot_student_results(student, scores, cohort_size):
95
89
# Set the right-hand Y-axis ticks and labels
96
90
ax2 = ax1 .twinx ()
97
91
98
- score_labels = [format_score (scores [k ].score , k ) for k in test_names ]
99
-
100
- # set the tick locations
92
+ # Set the tick locations
101
93
ax2 .set_yticks (pos )
102
- # make sure that the limits are set equally on both yaxis so the
103
- # ticks line up
94
+ # Set equal limits on both yaxis so that the ticks line up
104
95
ax2 .set_ylim (ax1 .get_ylim ())
105
96
106
- # set the tick labels
107
- ax2 .set_yticklabels (score_labels )
97
+ # Set the tick labels
98
+ ax2 .set_yticklabels ([ format_score ( scores [ k ]. score , k ) for k in test_names ] )
108
99
109
100
ax2 .set_ylabel ('Test Scores' )
110
101
@@ -146,9 +137,9 @@ def plot_student_results(student, scores, cohort_size):
146
137
color = clr , weight = 'bold' , clip_on = True )
147
138
rect_labels .append (label )
148
139
149
- # make the interactive mouse over give the bar title
140
+ # Make the interactive mouse over give the bar title
150
141
ax2 .fmt_ydata = format_ycursor
151
- # return all of the artists created
142
+ # Return all of the artists created
152
143
return {'fig' : fig ,
153
144
'ax' : ax1 ,
154
145
'ax_right' : ax2 ,
0 commit comments