1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| import numpy as np import matplotlib.pyplot as plt from matplotlib.cbook import get_sample_data
fname = get_sample_data('percent_bachelors_degrees_women_usa.csv', asfileobj=False) gender_degree_data = np.genfromtxt(fname, delimiter=',', names=True)
color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']
fig, ax = plt.subplots(1, 1, figsize=(12, 14))
ax.spines['top'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['left'].set_visible(False)
ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left()
fig.subplots_adjust(left=.06, right=.75, bottom=.02, top=.94)
ax.set_xlim(1969.5, 2011.1) ax.set_ylim(-0.25, 90)
plt.xticks(range(1970, 2011, 10), fontsize=14) plt.yticks(range(0, 91, 10), fontsize=14) ax.xaxis.set_major_formatter(plt.FuncFormatter('{:.0f}'.format)) ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.0f}%'.format))
plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
plt.tick_params(axis='both', which='both', bottom=False, top=False, labelbottom=True, left=False, right=False, labelleft=True)
majors = ['Health Professions', 'Public Administration', 'Education', 'Psychology', 'Foreign Languages', 'English', 'Communications\nand Journalism', 'Art and Performance', 'Biology', 'Agriculture', 'Social Sciences and History', 'Business', 'Math and Statistics', 'Architecture', 'Physical Sciences', 'Computer Science', 'Engineering']
y_offsets = {'Foreign Languages': 0.5, 'English': -0.5, 'Communications\nand Journalism': 0.75, 'Art and Performance': -0.25, 'Agriculture': 1.25, 'Social Sciences and History': 0.25, 'Business': -0.75, 'Math and Statistics': 0.75, 'Architecture': -0.75, 'Computer Science': 0.75, 'Engineering': -0.25}
for rank, column in enumerate(majors): column_rec_name = column.replace('\n', '_').replace(' ', '_')
line = plt.plot(gender_degree_data['Year'], gender_degree_data[column_rec_name], lw=2.5, color=color_sequence[rank])
y_pos = gender_degree_data[column_rec_name][-1] - 0.5
if column in y_offsets: y_pos += y_offsets[column]
plt.text(2011.5, y_pos, column, fontsize=14, color=color_sequence[rank])
fig.suptitle('Percentage of Bachelor\'s degrees conferred to women in ' 'the U.S.A. by major (1970-2011)\n', fontsize=18, ha='center')
plt.show()
|