In [1]:
%matplotlib inline
In [2]:
import matplotlib.pyplot as plt
In [3]:
# input data
data = [51,54,76,69,57,68,62,67,59]
labels = ['High\nPoverty\nSchools', 'Low\nIncome\nSchools',
'Low\nPoverty\nSchools', 'Higher\nIncome\nSchools',
'High\nMinority\nSchools', 'Low\nMinority\nSchools',
'Urban\nSchools', 'Suburban\nSchools', 'Rural\nSchools']
# \n is used for labeling without overlapping
text = ('51%','54%','76%','69%','57%','68%','62%','67%','59%')
In [4]:
# set the positions of each bar
pos = [0.5, 1, 2, 2.5, 4.5, 5.5, 7.5, 8.5, 9.5]
# set the width of each bar
width = 0.5
In [5]:
# plot bars
fig, ax = plt.subplots(figsize=(16,4))
# refer to RGB colors
# http://www.discoveryplayground.com/computer-programming-for-kids/rgb-colors/
plt.bar(pos[0], data[0], width, alpha=1, color='#b03060', edgecolor='none', align='center')
plt.bar(pos[1], data[1], width, alpha=0.5, color='#b03060', edgecolor='none', align='center')
plt.bar(pos[2], data[2], width, alpha=1, color='#b03060', edgecolor='none', align='center')
plt.bar(pos[3], data[3], width, alpha=0.5, color='#b03060', edgecolor='none', align='center')
plt.bar(pos[4:6], data[4:6], width, alpha=1, color='#ff8c00', edgecolor='none', align='center')
plt.bar(pos[6:], data[6:], width, alpha=1, color='#d02090', edgecolor='none', align='center')
# set the title, tick labels and grid of y-axis
ax.set_title('College Enrollment Rates in the First Fall after High School Graduation, Class of 2015, Public Non-Charter Schools')
ax.set_yticklabels(['0%','10%','20%','30%','40%','50%','60%','70%','80%','90%','100%'])
ax.yaxis.grid(True)
# add text located in the middle of each bar
# refer to Stackoverflow
# http://stackoverflow.com/questions/28931224/adding-value-labels-on-a-matplotlib-bar-chart
rects = ax.patches
for rect, label in zip(rects[:4], text[:4]):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2, height/2, label, ha='center', va='bottom', color='w')
for rect, label in zip(rects[4:6], text[4:6]):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2, height/2, label, ha='center', va='bottom', color='k')
for rect, label in zip(rects[6:], text[6:]):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2, height/2, label, ha='center', va='bottom', color='w')
# set the range and tick for x-axis
plt.xlim(min(pos)-width, max(pos)+width)
plt.xticks(pos, labels)
plt.savefig('Figure of High School Graduation.png')
In [ ]: