8000 Update barchart_demo. by anntzer · Pull Request #16807 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Update barchart_demo. #16807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions examples/statistics/barchart_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,31 @@
# 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):
"""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):
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):
Expand All @@ -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')

Expand All @@ -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')

Expand Down Expand Up @@ -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,
Expand Down
0