From d13608e95ea450d0c740855d5e31b157ea7470d2 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 17 Mar 2020 20:31:34 +0100 Subject: [PATCH] Update barchart_demo. Replace "meta information" by "units" (which describes what it is better). Capitalize comments. --- examples/statistics/barchart_demo.py | 41 +++++++++++----------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/examples/statistics/barchart_demo.py b/examples/statistics/barchart_demo.py index 52a9c1bf8c0b..a8d78b96363c 100644 --- a/examples/statistics/barchart_demo.py +++ b/examples/statistics/barchart_demo.py @@ -29,7 +29,7 @@ # 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', ''])) +test_units = dict(zip(test_names, ['laps', 'sec', 'min:sec', 'sec', ''])) def attach_ordinal(num): @@ -37,7 +37,6 @@ def attach_ordinal(num): 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'}: @@ -45,20 +44,16 @@ def attach_ordinal(num): return v + suffixes[v[-1]] -def format_score(scr, test): +def format_score(score, 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 + Create score labels for the right y-axis as the test name followed by the + measurement unit (if any), split over two lines. """ - md = test_meta[test] - if md: - return '{0}\n{1}'.format(scr, md) - else: - return scr + unit = test_units[test] + if unit: + return f'{score}\n{unit}' + else: # If no unit, don't include a newline, so that label stays centered. + return score def format_ycursor(y): @@ -70,8 +65,7 @@ def format_ycursor(y): def plot_student_results(student, scores, cohort_size): - # create the figure - fig, ax1 = plt.subplots(figsize=(9, 7)) + fig, ax1 = plt.subplots(figsize=(9, 7)) # Create the figure fig.subplots_adjust(left=0.115, right=0.88) fig.canvas.set_window_title('Eldorado K-8 Fitness Chart') @@ -95,16 +89,13 @@ def plot_student_results(student, scores, cohort_size): # Set the right-hand Y-axis ticks and labels ax2 = ax1.twinx() - score_labels = [format_score(scores[k].score, k) for k in test_names] - - # set the tick locations + # Set the tick locations ax2.set_yticks(pos) - # make sure that the limits are set equally on both yaxis so the - # ticks line up + # Set equal limits on both yaxis so that the ticks line up ax2.set_ylim(ax1.get_ylim()) - # set the tick labels - ax2.set_yticklabels(score_labels) + # Set the tick labels + ax2.set_yticklabels([format_score(scores[k].score, k) for k in test_names]) ax2.set_ylabel('Test Scores') @@ -146,9 +137,9 @@ def plot_student_results(student, scores, cohort_size): color=clr, weight='bold', clip_on=True) rect_labels.append(label) - # make the interactive mouse over give the bar title + # Make the interactive mouse over give the bar title ax2.fmt_ydata = format_ycursor - # return all of the artists created + # Return all of the artists created return {'fig': fig, 'ax': ax1, 'ax_right': ax2,