8000 Merge pull request #16807 from anntzer/barchart_demo · matplotlib/matplotlib@71163e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71163e9

Browse files
authored
Merge pull request #16807 from anntzer/barchart_demo
Update barchart_demo.
2 parents dfc830d + d13608e commit 71163e9

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

examples/statistics/barchart_demo.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,31 @@
2929
# GLOBAL CONSTANTS
3030
test_names = ['Pacer Test', 'Flexed Arm\n Hang', 'Mile Run', 'Agility',
3131
'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', '']))
3333

3434

3535
def attach_ordinal(num):
3636
"""Convert an integer to an ordinal string, e.g. 2 -> '2nd'."""
3737
suffixes = {str(i): v
3838
for i, v in enumerate(['th', 'st', 'nd', 'rd', 'th',
3939
'th', 'th', 'th', 'th', 'th'])}
40-
4140
v = str(num)
4241
# special case early teens
4342
if v in {'11', '12', '13'}:
4443
return v + 'th'
4544
return v + suffixes[v[-1]]
4645

4746

48-
def format_score(scr, test):
47+
def format_score(score, test):
4948
"""
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.
5651
"""
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
6257

6358

6459
def format_ycursor(y):
@@ -70,8 +65,7 @@ def format_ycursor(y):
7065

7166

7267
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
7569
fig.subplots_adjust(left=0.115, right=0.88)
7670
fig.canvas.set_window_title('Eldorado K-8 Fitness Chart')
7771

@@ -95,16 +89,13 @@ def plot_student_results(student, scores, cohort_size):
9589
# Set the right-hand Y-axis ticks and labels
9690
ax2 = ax1.twinx()
9791

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
10193
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
10495
ax2.set_ylim(ax1.get_ylim())
10596

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])
10899

109100
ax2.set_ylabel('Test Scores')
110101

@@ -146,9 +137,9 @@ def plot_student_results(student, scores, cohort_size):
146137
color=clr, weight='bold', clip_on=True)
147138
rect_labels.append(label)
148139

149-
# make the interactive mouse over give the bar title
140+
# Make the interactive mouse over give the bar title
150141
ax2.fmt_ydata = format_ycursor
151-
# return all of the artists created
142+
# Return all of the artists created
152143
return {'fig': fig,
153144
'ax': ax1,
154145
'ax_right': ax2,

0 commit comments

Comments
 (0)
0