Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 952a452

Browse files
committed
DOC: update example
Update example to use 'modern' idioms
1 parent 911f349 commit 952a452

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

examples/pylab_examples/barchart_demo2.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,32 @@
2929
plt.subplots_adjust(left=0.115, right=0.88)
3030
fig.canvas.set_window_title('Eldorado K-8 Fitness Chart')
3131
pos = np.arange(numTests) + 0.5 # Center bars on the Y-axis ticks
32-
rects = ax1.barh(pos, rankings, align='center', height=0.5, color='m')
3332

34-
ax1.axis([0, 100, 0, 5])
35-
plt.yticks(pos, testNames)
33+
rects = ax1.barh(pos, rankings, align='center', height=0.5, color='m',
34+
tick_label=testNames)
35+
3636
ax1.set_title('Johnny Doe')
37-
plt.text(50, -0.5, 'Cohort Size: ' + str(cohortSize),
38-
horizontalalignment='center', size='small')
37+
38+
ax1.set_xlim([0, 100])
39+
ax1.xaxis.set_major_locator(MaxNLocator(11))
40+
ax1.xaxis.grid(True, linestyle='--', which='major', color='grey', alpha=.25)
41+
42+
# Plot a solid vertical gridline to highlight the median position
43+
ax1.axvline(50, color='grey', alpha=0.25)
44+
45+
ax1.text(.5, -.07, 'Cohort Size: ' + str(cohortSize),
46+
horizontalalignment='center', size='small',
47+
transform=ax1.transAxes)
3948

4049
# Set the right-hand Y-axis ticks and labels and set X-axis tick marks at the
4150
# deciles
4251
ax2 = ax1.twinx()
43-
ax2.plot([100, 100], [0, 5], 'white', alpha=0.1)
44-
ax2.xaxis.set_major_locator(MaxNLocator(11))
45-
xticks = plt.setp(ax2, xticklabels=['0', '10', '20', '30', '40', '50', '60',
46-
'70', '80', '90', '100'])
47-
ax2.xaxis.grid(True, linestyle='--', which='major', color='grey',
48-
alpha=0.25)
49-
# Plot a solid vertical gridline to highlight the median position
50-
plt.plot([50, 50], [0, 5], 'grey', alpha=0.25)
5152

5253
# Build up the score labels for the right Y-axis by first appending a carriage
5354
# return to each string and then tacking on the appropriate meta information
5455
# (i.e., 'laps' vs 'seconds'). We want the labels centered on the ticks, so if
5556
# there is no meta info (like for pushups) then don't add the carriage return to
5657
# the string
57-
58-
5958
def withnew(i, scr):
6059
if testMeta[i] != '':
6160
return '%s\n' % scr
@@ -71,7 +70,6 @@ def withnew(i, scr):
7170
# make sure that the limits are set equally on both yaxis so the ticks line up
7271
ax2.set_ylim(ax1.get_ylim())
7372

74-
7573
ax2.set_ylabel('Test Scores')
7674
# Make list of numerical suffixes corresponding to position in a list
7775
# 0 1 2 3 4 5 6 7 8 9
@@ -108,6 +106,7 @@ def withnew(i, scr):
108106
# Center the text vertically in the bar
109107
yloc = rect.get_y() + rect.get_height()/2.0
110108
ax1.text(xloc, yloc, rankStr, horizontalalignment=align,
111-
verticalalignment='center', color=clr, weight='bold')
109+
verticalalignment='center', color=clr, weight='bold',
110+
clip_on=True)
112111

113112
plt.show()

0 commit comments

Comments
 (0)