|
13 | 13 | import pylab |
14 | 14 | from matplotlib.ticker import MaxNLocator |
15 | 15 |
|
16 | | - |
17 | | - |
18 | 16 | student = 'Johnny Doe' |
19 | 17 | grade = 2 |
20 | 18 | gender = 'boy' |
21 | | -cohortSize = 62 #The number of other 2nd grade boys |
| 19 | +cohortSize = 62 # The number of other 2nd grade boys |
22 | 20 |
|
23 | 21 | numTests = 5 |
24 | 22 | testNames = ['Pacer Test', 'Flexed Arm\n Hang', 'Mile Run', 'Agility', |
25 | | - 'Push Ups'] |
| 23 | + 'Push Ups'] |
26 | 24 | testMeta = ['laps', 'sec', 'min:sec', 'sec', ''] |
27 | 25 | scores = ['7', '48', '12:52', '17', '14'] |
28 | 26 | rankings = np.round(np.random.uniform(0, 1, numTests)*100, 0) |
29 | 27 |
|
30 | 28 |
|
31 | | -fig, ax1 = plt.subplots(figsize=(9,7)) |
| 29 | +fig, ax1 = plt.subplots(figsize=(9, 7)) |
32 | 30 | plt.subplots_adjust(left=0.115, right=0.88) |
33 | 31 | fig.canvas.set_window_title('Eldorado K-8 Fitness Chart') |
34 | | -pos = np.arange(numTests)+0.5 #Center bars on the Y-axis ticks |
| 32 | +pos = np.arange(numTests)+0.5 # Center bars on the Y-axis ticks |
35 | 33 | rects = ax1.barh(pos, rankings, align='center', height=0.5, color='m') |
36 | 34 |
|
37 | | -ax1.axis([0,100,0,5]) |
| 35 | +ax1.axis([0, 100, 0, 5]) |
38 | 36 | pylab.yticks(pos, testNames) |
39 | 37 | ax1.set_title('Johnny Doe') |
40 | 38 | plt.text(50, -0.5, 'Cohort Size: ' + str(cohortSize), |
41 | | - horizontalalignment='center', size='small') |
| 39 | + horizontalalignment='center', size='small') |
42 | 40 |
|
43 | 41 | # Set the right-hand Y-axis ticks and labels and set X-axis tick marks at the |
44 | 42 | # deciles |
45 | 43 | ax2 = ax1.twinx() |
46 | | -ax2.plot([100,100], [0, 5], 'white', alpha=0.1) |
| 44 | +ax2.plot([100, 100], [0, 5], 'white', alpha=0.1) |
47 | 45 | ax2.xaxis.set_major_locator(MaxNLocator(11)) |
48 | | -xticks = pylab.setp(ax2, xticklabels=['0','10','20','30','40','50','60', |
49 | | -'70', |
50 | | - '80','90','100']) |
| 46 | +xticks = pylab.setp(ax2, xticklabels=['0', '10', '20', '30', '40', '50', '60', |
| 47 | + '70', '80', '90', '100']) |
51 | 48 | ax2.xaxis.grid(True, linestyle='--', which='major', color='grey', |
52 | 49 | alpha=0.25) |
53 | 50 | #Plot a solid vertical gridline to highlight the median position |
54 | | -plt.plot([50,50], [0, 5], 'grey', alpha=0.25) |
| 51 | +plt.plot([50, 50], [0, 5], 'grey', alpha=0.25) |
55 | 52 |
|
56 | 53 | # Build up the score labels for the right Y-axis by first appending a carriage |
57 | 54 | # return to each string and then tacking on the appropriate meta information |
58 | 55 | # (i.e., 'laps' vs 'seconds'). We want the labels centered on the ticks, so if |
59 | 56 | # there is no meta info (like for pushups) then don't add the carriage return to |
60 | 57 | # the string |
61 | 58 |
|
| 59 | + |
62 | 60 | def withnew(i, scr): |
63 | | - if testMeta[i] != '' : return '%s\n'%scr |
64 | | - else: return scr |
65 | | -scoreLabels = [withnew(i, scr) for i,scr in enumerate(scores)] |
66 | | -scoreLabels = [i+j for i,j in zip(scoreLabels, testMeta)] |
67 | | -pylab.yticks(pos, scoreLabels) |
| 61 | + if testMeta[i] != '': |
| 62 | + return '%s\n' % scr |
| 63 | + else: |
| 64 | + return scr |
| 65 | + |
| 66 | +scoreLabels = [withnew(i, scr) for i, scr in enumerate(scores)] |
| 67 | +scoreLabels = [i+j for i, j in zip(scoreLabels, testMeta)] |
| 68 | +# set the tick locations |
| 69 | +ax2.set_yticks(pos) |
| 70 | +# set the tick labels |
| 71 | +ax2.set_yticklabels(scoreLabels) |
| 72 | +# make sure that the limits are set equally on both yaxis so the ticks line up |
| 73 | +ax2.set_ylim(ax1.get_ylim()) |
| 74 | + |
| 75 | + |
68 | 76 | ax2.set_ylabel('Test Scores') |
69 | 77 | #Make list of numerical suffixes corresponding to position in a list |
70 | | -# 0 1 2 3 4 5 6 7 8 9 |
71 | | -suffixes =['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'] |
72 | | -ax2.set_xlabel('Percentile Ranking Across ' + str(grade) + suffixes[grade] \ |
| 78 | +# 0 1 2 3 4 5 6 7 8 9 |
| 79 | +suffixes = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'] |
| 80 | +ax2.set_xlabel('Percentile Ranking Across ' + str(grade) + suffixes[grade] |
73 | 81 | + ' Grade ' + gender.title() + 's') |
74 | 82 |
|
75 | 83 | # Lastly, write in the ranking inside each bar to aid in interpretation |
76 | 84 | for rect in rects: |
77 | | - # Rectangle widths are already integer-valued but are floating |
78 | | - # type, so it helps to remove the trailing decimal point and 0 by |
79 | | - # converting width to int type |
80 | | - width = int(rect.get_width()) |
81 | | - |
82 | | - # Figure out what the last digit (width modulo 10) so we can add |
83 | | - # the appropriate numerical suffix (e.g., 1st, 2nd, 3rd, etc) |
84 | | - lastDigit = width % 10 |
85 | | - # Note that 11, 12, and 13 are special cases |
86 | | - if (width == 11) or (width == 12) or (width == 13): |
87 | | - suffix = 'th' |
88 | | - else: |
89 | | - suffix = suffixes[lastDigit] |
90 | | - |
91 | | - rankStr = str(width) + suffix |
92 | | - if (width < 5): # The bars aren't wide enough to print the ranking inside |
93 | | - xloc = width + 1 # Shift the text to the right side of the right edge |
94 | | - clr = 'black' # Black against white background |
95 | | - align = 'left' |
96 | | - else: |
97 | | - xloc = 0.98*width # Shift the text to the left side of the right edge |
98 | | - clr = 'white' # White on magenta |
99 | | - align = 'right' |
100 | | - |
101 | | - yloc = rect.get_y()+rect.get_height()/2.0 #Center the text vertically in the bar |
102 | | - ax1.text(xloc, yloc, rankStr, horizontalalignment=align, |
| 85 | + # Rectangle widths are already integer-valued but are floating |
| 86 | + # type, so it helps to remove the trailing decimal point and 0 by |
| 87 | + # converting width to int type |
| 88 | + width = int(rect.get_width()) |
| 89 | + |
| 90 | + # Figure out what the last digit (width modulo 10) so we can add |
| 91 | + # the appropriate numerical suffix (e.g., 1st, 2nd, 3rd, etc) |
| 92 | + lastDigit = width % 10 |
| 93 | + # Note that 11, 12, and 13 are special cases |
| 94 | + if (width == 11) or (width == 12) or (width == 13): |
| 95 | + suffix = 'th' |
| 96 | + else: |
| 97 | + suffix = suffixes[lastDigit] |
| 98 | + |
| 99 | + rankStr = str(width) + suffix |
| 100 | + if (width < 5): # The bars aren't wide enough to print the ranking inside |
| 101 | + xloc = width + 1 # Shift the text to the right side of the right edge |
| 102 | + clr = 'black' # Black against white background |
| 103 | + align = 'left' |
| 104 | + else: |
| 105 | + xloc = 0.98*width # Shift the text to the left side of the right edge |
| 106 | + clr = 'white' # White on magenta |
| 107 | + align = 'right' |
| 108 | + |
| 109 | + # Center the text vertically in the bar |
| 110 | + yloc = rect.get_y()+rect.get_height()/2.0 |
| 111 | + ax1.text(xloc, yloc, rankStr, horizontalalignment=align, |
103 | 112 | verticalalignment='center', color=clr, weight='bold') |
104 | 113 |
|
105 | 114 | plt.show() |
106 | | - |
|
0 commit comments