|
14 | 14 | # You typically want your plot to be ~1.33x wider than tall. This plot
|
15 | 15 | # is a rare exception because of the number of lines being plotted on it.
|
16 | 16 | # Common sizes: (10, 7.5) and (12, 9)
|
17 |
| -plt.figure(figsize=(12, 14)) |
| 17 | +fig, ax = plt.subplots(1, 1, figsize=(12, 14)) |
18 | 18 |
|
19 |
| -# Remove the plot frame lines. They are unnecessary chartjunk. |
20 |
| -ax = plt.subplot(111) |
| 19 | +# Remove the plot frame lines. They are unnecessary here. |
21 | 20 | ax.spines['top'].set_visible(False)
|
22 | 21 | ax.spines['bottom'].set_visible(False)
|
23 | 22 | ax.spines['right'].set_visible(False)
|
24 | 23 | ax.spines['left'].set_visible(False)
|
25 | 24 |
|
26 |
| -# Ensure that the axis ticks only show up on the bottom and left of the plot. |
27 |
| -# Ticks on the right and top of the plot are generally unnecessary chartjunk. |
| 25 | +# Ensure that the axis ticks only show up on the bottom and left of the plot. |
| 26 | +# Ticks on the right and top of the plot are generally unnecessary. |
28 | 27 | ax.get_xaxis().tick_bottom()
|
29 | 28 | ax.get_yaxis().tick_left()
|
30 | 29 |
|
|
36 | 35 | # Make sure your axis ticks are large enough to be easily read.
|
37 | 36 | # You don't want your viewers squinting to read your plot.
|
38 | 37 | plt.xticks(range(1970, 2011, 10), fontsize=14)
|
39 |
| -plt.yticks(range(0, 91, 10), ['{}%'.format(x) for x in range(0, 91, 10)], fontsize=14) |
| 38 | +plt.yticks(range(0, 91, 10), ['{}%'.format(x) |
| 39 | + for x in range(0, 91, 10)], fontsize=14) |
40 | 40 |
|
41 |
| -# Provide tick lines across the plot to help your viewers trace along |
42 |
| -# the axis ticks. Make sure that the lines are light and small so they |
43 |
| -# don't obscure the primary data lines. |
44 |
| -for y in range(10, 91, 10): |
| 41 | +# Provide tick lines across the plot to help your viewers trace along |
| 42 | +# the axis ticks. Make sure that the lines are light and small so they |
| 43 | +# don't obscure the primary data lines. |
| 44 | +for y in range(10, 91, 10): |
45 | 45 | plt.plot(range(1969, 2012), [y] * len(range(1969, 2012)), '--',
|
46 |
| - lw=0.5, color='black', alpha=0.3) |
| 46 | + lw=0.5, color='black', alpha=0.3) |
47 | 47 |
|
48 |
| -# Remove the tick marks; they are unnecessary with the tick lines we just plotted. |
49 |
| -plt.tick_params(axis='both', which='both', bottom='off', top='off', |
50 |
| - labelbottom='on', left='off', right='off', labelleft='on') |
| 48 | +# Remove the tick marks; they are unnecessary with the tick lines we just |
| 49 | +# plotted. |
| 50 | +plt.tick_params(axis='both', which='both', bottom='off', top='off', |
| 51 | + labelbottom='on', left='off', right='off', labelleft='on') |
51 | 52 |
|
52 | 53 | # Now that the plot is prepared, it's time to actually plot the data!
|
53 | 54 | # Note that I plotted the majors in order of the highest % in the final year.
|
|
91 | 92 | # Note that if the title is descriptive enough, it is unnecessary to include
|
92 | 93 | # axis labels; they are self-evident, in this plot's case.
|
93 | 94 | plt.title('Percentage of Bachelor\'s degrees conferred to women in '
|
94 |
| - 'the U.S.A. by major (1970-2011)\n', fontsize=18, ha='center') |
| 95 | + 'the U.S.A. by major (1970-2011)\n', fontsize=18, ha='center') |
95 | 96 |
|
96 | 97 | # Finally, save the figure as a PNG.
|
97 | 98 | # You can also save it as a PDF, JPEG, etc.
|
|
0 commit comments