Skip to content

Commit ff62378

Browse files
tacaswellmdboom
authored andcommitted
pep8 fixes
1 parent 1c7fe93 commit ff62378

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

examples/pylab_examples/barchart_demo2.py

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,94 +13,96 @@
1313
import pylab
1414
from matplotlib.ticker import MaxNLocator
1515

16-
17-
1816
student = 'Johnny Doe'
1917
grade = 2
2018
gender = 'boy'
21-
cohortSize = 62 #The number of other 2nd grade boys
19+
cohortSize = 62 # The number of other 2nd grade boys
2220

2321
numTests = 5
2422
testNames = ['Pacer Test', 'Flexed Arm\n Hang', 'Mile Run', 'Agility',
25-
'Push Ups']
23+
'Push Ups']
2624
testMeta = ['laps', 'sec', 'min:sec', 'sec', '']
2725
scores = ['7', '48', '12:52', '17', '14']
2826
rankings = np.round(np.random.uniform(0, 1, numTests)*100, 0)
2927

3028

31-
fig, ax1 = plt.subplots(figsize=(9,7))
29+
fig, ax1 = plt.subplots(figsize=(9, 7))
3230
plt.subplots_adjust(left=0.115, right=0.88)
3331
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
3533
rects = ax1.barh(pos, rankings, align='center', height=0.5, color='m')
3634

37-
ax1.axis([0,100,0,5])
35+
ax1.axis([0, 100, 0, 5])
3836
pylab.yticks(pos, testNames)
3937
ax1.set_title('Johnny Doe')
4038
plt.text(50, -0.5, 'Cohort Size: ' + str(cohortSize),
41-
horizontalalignment='center', size='small')
39+
horizontalalignment='center', size='small')
4240

4341
# Set the right-hand Y-axis ticks and labels and set X-axis tick marks at the
4442
# deciles
4543
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)
4745
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'])
5148
ax2.xaxis.grid(True, linestyle='--', which='major', color='grey',
5249
alpha=0.25)
5350
#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)
5552

5653
# Build up the score labels for the right Y-axis by first appending a carriage
5754
# return to each string and then tacking on the appropriate meta information
5855
# (i.e., 'laps' vs 'seconds'). We want the labels centered on the ticks, so if
5956
# there is no meta info (like for pushups) then don't add the carriage return to
6057
# the string
6158

59+
6260
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)]
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)]
6768
pylab.yticks(pos, scoreLabels)
6869
ax2.set_ylabel('Test Scores')
6970
#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] \
71+
# 0 1 2 3 4 5 6 7 8 9
72+
suffixes = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
73+
ax2.set_xlabel('Percentile Ranking Across ' + str(grade) + suffixes[grade]
7374
+ ' Grade ' + gender.title() + 's')
7475

76+
7577
# Lastly, write in the ranking inside each bar to aid in interpretation
7678
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,
79+
# Rectangle widths are already integer-valued but are floating
80+
# type, so it helps to remove the trailing decimal point and 0 by
81+
# converting width to int type
82+
width = int(rect.get_width())
83+
84+
# Figure out what the last digit (width modulo 10) so we can add
85+
# the appropriate numerical suffix (e.g., 1st, 2nd, 3rd, etc)
86+
lastDigit = width % 10
87+
# Note that 11, 12, and 13 are special cases
88+
if (width == 11) or (width == 12) or (width == 13):
89+
suffix = 'th'
90+
else:
91+
suffix = suffixes[lastDigit]
92+
93+
rankStr = str(width) + suffix
94+
if (width < 5): # The bars aren't wide enough to print the ranking inside
95+
xloc = width + 1 # Shift the text to the right side of the right edge
96+
clr = 'black' # Black against white background
97+
align = 'left'
98+
else:
99+
xloc = 0.98*width # Shift the text to the left side of the right edge
100+
clr = 'white' # White on magenta
101+
align = 'right'
102+
103+
# Center the text vertically in the bar
104+
yloc = rect.get_y()+rect.get_height()/2.0
105+
ax1.text(xloc, yloc, rankStr, horizontalalignment=align,
103106
verticalalignment='center', color=clr, weight='bold')
104107

105108
plt.show()
106-

0 commit comments

Comments
 (0)