|
47 | 47 | ax.set_xlim(1969.5, 2011.1)
|
48 | 48 | ax.set_ylim(-0.25, 90)
|
49 | 49 |
|
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) |
| 50 | +# Set a fixed location and format for ticks. |
| 51 | +ax.set_xticks(range(1970, 2011, 10)) |
| 52 | +ax.set_yticks(range(0, 91, 10)) |
54 | 53 | ax.xaxis.set_major_formatter(plt.FuncFormatter('{:.0f}'.format))
|
55 | 54 | ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.0f}%'.format))
|
56 | 55 |
|
57 | 56 | # Provide tick lines across the plot to help your viewers trace along
|
58 | 57 | # the axis ticks. Make sure that the lines are light and small so they
|
59 | 58 | # don't obscure the primary data lines.
|
60 |
| -plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3) |
| 59 | +ax.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3) |
61 | 60 |
|
62 | 61 | # 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) |
| 62 | +# plotted. Make sure your axis ticks are large enough to be easily read. |
| 63 | +# You don't want your viewers squinting to read your plot. |
| 64 | +ax.tick_params(axis='both', which='both', labelsize=14, |
| 65 | + bottom=False, top=False, labelbottom=True, |
| 66 | + left=False, right=False, labelleft=True) |
66 | 67 |
|
67 | 68 | # Now that the plot is prepared, it's time to actually plot the data!
|
68 | 69 | # Note that I plotted the majors in order of the highest % in the final year.
|
|
84 | 85 | # Plot each line separately with its own color.
|
85 | 86 | column_rec_name = column.replace('\n', '_').replace(' ', '_')
|
86 | 87 |
|
87 |
| - line = plt.plot(gender_degree_data['Year'], |
88 |
| - gender_degree_data[column_rec_name], |
89 |
| - lw=2.5, |
90 |
| - color=color_sequence[rank]) |
| 88 | + line = ax.plot(gender_degree_data['Year'], |
| 89 | + gender_degree_data[column_rec_name], |
| 90 | + lw=2.5, |
| 91 | + color=color_sequence[rank]) |
91 | 92 |
|
92 | 93 | # Add a text label to the right end of every line. Most of the code below
|
93 | 94 | # is adding specific offsets y position because some labels overlapped.
|
|
98 | 99 |
|
99 | 100 | # Again, make sure that all labels are large enough to be easily read
|
100 | 101 | # by the viewer.
|
101 |
| - plt.text(2011.5, y_pos, column, fontsize=14, color=color_sequence[rank]) |
| 102 | + ax.text(2011.5, y_pos, column, fontsize=14, color=color_sequence[rank]) |
102 | 103 |
|
103 | 104 | # Make the title big enough so it spans the entire plot, but don't make it
|
104 | 105 | # so big that it requires two lines to show.
|
|
111 | 112 | # Finally, save the figure as a PNG.
|
112 | 113 | # You can also save it as a PDF, JPEG, etc.
|
113 | 114 | # Just change the file extension in this call.
|
114 |
| -# plt.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight') |
| 115 | +# fig.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight') |
115 | 116 | plt.show()
|
0 commit comments