|  | 
| 19 | 19 |                         asfileobj=False) | 
| 20 | 20 | gender_degree_data = np.genfromtxt(fname, delimiter=',', names=True) | 
| 21 | 21 | 
 | 
| 22 |  | -# These are the colors that will be used in the plot | 
| 23 |  | -color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', | 
| 24 |  | -                  '#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5', | 
| 25 |  | -                  '#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f', | 
| 26 |  | -                  '#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5'] | 
| 27 |  | - | 
| 28 | 22 | # You typically want your plot to be ~1.33x wider than tall. This plot | 
| 29 | 23 | # is a rare exception because of the number of lines being plotted on it. | 
| 30 | 24 | # Common sizes: (10, 7.5) and (12, 9) | 
| 31 | 25 | fig, ax = plt.subplots(1, 1, figsize=(12, 14)) | 
| 32 | 26 | 
 | 
|  | 27 | +# These are the colors that will be used in the plot | 
|  | 28 | +ax.set_prop_cycle(color=[ | 
|  | 29 | +    '#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a', | 
|  | 30 | +    '#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94', | 
|  | 31 | +    '#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d', | 
|  | 32 | +    '#17becf', '#9edae5']) | 
|  | 33 | + | 
| 33 | 34 | # Remove the plot frame lines. They are unnecessary here. | 
| 34 | 35 | ax.spines['top'].set_visible(False) | 
| 35 | 36 | ax.spines['bottom'].set_visible(False) | 
|  | 
| 47 | 48 | ax.set_xlim(1969.5, 2011.1) | 
| 48 | 49 | ax.set_ylim(-0.25, 90) | 
| 49 | 50 | 
 | 
| 50 |  | -# Make sure your axis ticks are large enough to be easily read. | 
| 51 |  | -# You don't want your viewers squinting to read your plot. | 
| 52 |  | -plt.xticks(range(1970, 2011, 10), fontsize=14) | 
| 53 |  | -plt.yticks(range(0, 91, 10), fontsize=14) | 
|  | 51 | +# Set a fixed location and format for ticks. | 
|  | 52 | +ax.set_xticks(range(1970, 2011, 10)) | 
|  | 53 | +ax.set_yticks(range(0, 91, 10)) | 
| 54 | 54 | ax.xaxis.set_major_formatter(plt.FuncFormatter('{:.0f}'.format)) | 
| 55 | 55 | ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.0f}%'.format)) | 
| 56 | 56 | 
 | 
| 57 | 57 | # Provide tick lines across the plot to help your viewers trace along | 
| 58 | 58 | # the axis ticks. Make sure that the lines are light and small so they | 
| 59 | 59 | # don't obscure the primary data lines. | 
| 60 |  | -plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3) | 
|  | 60 | +ax.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3) | 
| 61 | 61 | 
 | 
| 62 | 62 | # Remove the tick marks; they are unnecessary with the tick lines we just | 
| 63 |  | -# plotted. | 
| 64 |  | -plt.tick_params(axis='both', which='both', bottom=False, top=False, | 
| 65 |  | -                labelbottom=True, left=False, right=False, labelleft=True) | 
|  | 63 | +# plotted. Make sure your axis ticks are large enough to be easily read. | 
|  | 64 | +# You don't want your viewers squinting to read your plot. | 
|  | 65 | +ax.tick_params(axis='both', which='both', labelsize=14, | 
|  | 66 | +               bottom=False, top=False, labelbottom=True, | 
|  | 67 | +               left=False, right=False, labelleft=True) | 
| 66 | 68 | 
 | 
| 67 | 69 | # Now that the plot is prepared, it's time to actually plot the data! | 
| 68 | 70 | # Note that I plotted the majors in order of the highest % in the final year. | 
|  | 
| 80 | 82 |              'Math and Statistics': 0.75, 'Architecture': -0.75, | 
| 81 | 83 |              'Computer Science': 0.75, 'Engineering': -0.25} | 
| 82 | 84 | 
 | 
| 83 |  | -for rank, column in enumerate(majors): | 
|  | 85 | +for column in majors: | 
| 84 | 86 |     # Plot each line separately with its own color. | 
| 85 | 87 |     column_rec_name = column.replace('\n', '_').replace(' ', '_') | 
| 86 | 88 | 
 | 
| 87 |  | -    line = plt.plot(gender_degree_data['Year'], | 
| 88 |  | -                    gender_degree_data[column_rec_name], | 
| 89 |  | -                    lw=2.5, | 
| 90 |  | -                    color=color_sequence[rank]) | 
|  | 89 | +    line, = ax.plot('Year', column_rec_name, data=gender_degree_data, | 
|  | 90 | +                    lw=2.5) | 
| 91 | 91 | 
 | 
| 92 | 92 |     # Add a text label to the right end of every line. Most of the code below | 
| 93 | 93 |     # is adding specific offsets y position because some labels overlapped. | 
|  | 
| 98 | 98 | 
 | 
| 99 | 99 |     # Again, make sure that all labels are large enough to be easily read | 
| 100 | 100 |     # by the viewer. | 
| 101 |  | -    plt.text(2011.5, y_pos, column, fontsize=14, color=color_sequence[rank]) | 
|  | 101 | +    ax.text(2011.5, y_pos, column, fontsize=14, color=line.get_color()) | 
| 102 | 102 | 
 | 
| 103 | 103 | # Make the title big enough so it spans the entire plot, but don't make it | 
| 104 | 104 | # so big that it requires two lines to show. | 
|  | 
| 111 | 111 | # Finally, save the figure as a PNG. | 
| 112 | 112 | # You can also save it as a PDF, JPEG, etc. | 
| 113 | 113 | # Just change the file extension in this call. | 
| 114 |  | -# plt.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight') | 
|  | 114 | +# fig.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight') | 
| 115 | 115 | plt.show() | 
0 commit comments