8000 Merge pull request #4611 from rhiever/master · matplotlib/matplotlib@5795f8d · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5795f8d

Browse files
committed
Merge pull request #4611 from rhiever/master
Add % bachelors degrees plot example
2 parents 20e297f + da2842e commit 5795f8d

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import matplotlib.pyplot as plt
2+
from matplotlib.mlab import csv2rec
3+
from matplotlib.cbook import get_sample_data
4+
5+
fname = get_sample_data('percent_bachelors_degrees_women_usa.csv')
6+
gender_degree_data = csv2rec(fname)
7+
8+
# These are the colors that will be used in the plot
9+
color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c',
10+
'#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5',
11+
'#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f',
12+
'#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']
13+
14+
# You typically want your plot to be ~1.33x wider than tall. This plot
15+
# is a rare exception because of the number of lines being plotted on it.
16+
# Common sizes: (10, 7.5) and (12, 9)
17+
fig, ax = plt.subplots(1, 1, figsize=(12, 14))
18+
19+
# Remove the plot frame lines. They are unnecessary here.
20+
ax.spines['top'].set_visible(False)
21+
ax.spines['bottom'].set_visible(False)
22+
ax.spines['right'].set_visible(False)
23+
ax.spines['left'].set_visible(False)
24+
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.
27+
ax.get_xaxis().tick_bottom()
28+
ax.get_yaxis().tick_left()
29+
30+
# Limit the range of the plot to only where the data is.
31+
# Avoid unnecessary whitespace.
32+
plt.xlim(1968.5, 2011.1)
33+
plt.ylim(-0.25, 90)
34+
35+
# Make sure your axis ticks are large enough to be easily read.
36+
# You don't want your viewers squinting to read your plot.
37+
plt.xticks(range(1970, 2011, 10), fontsize=14)
38+
plt.yticks(range(0, 91, 10), ['{}%'.format(x)
39+
for x in range(0, 91, 10)], fontsize=14)
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):
45+
plt.plot(range(1969, 2012), [y] * len(range(1969, 2012)), '--',
46+
lw=0.5, color='black', alpha=0.3)
47+
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')
52+
53+
# Now that the plot is prepared, it's time to actually plot the data!
54+
# Note that I plotted the majors in order of the highest % in the final year.
55+
majors = ['Health Professions', 'Public Administration', 'Education',
56+
'Psychology', 'Foreign Languages', 'English',
57+
'Communications\nand Journalism', 'Art and Performance', 'Biology',
58+
'Agriculture', 'Social Sciences and History', 'Business',
59+
'Math and Statistics', 'Architecture', 'Physical Sciences',
60+
'Computer Science', 'Engineering']
61+
62+
y_offsets = {'Foreign Languages': 0.5, 'English': -0.5,
63+
'Communications\nand Journalism': 0.75,
64+
'Art and Performance': -0.25, 'Agriculture': 1.25,
65+
'Social Sciences and History': 0.25, 'Business': -0.75,
66+
'Math and Statistics': 0.75, 'Architecture': -0.75,
67+
'Computer Science': 0.75, 'Engineering': -0.25}
68+
69+
for rank, column in enumerate(majors):
70+
# Plot each line separately with its own color.
71+
column_rec_name = column.replace('\n', '_').replace(' ', '_').lower()
72+
73+
line = plt.plot(gender_degree_data.year,
74+
gender_degree_data[column_rec_name],
75+
lw=2.5,
76+
color=color_sequence[rank])
77+
78+
# Add a text label to the right end of every line. Most of the code below
79+
# is adding specific offsets y position because some labels overlapped.
80+
y_pos = gender_degree_data[column_rec_name][-1] - 0.5
81+
82+
if column in y_offsets:
83+
y_pos += y_offsets[column]
84+
85+
# Again, make sure that all labels are large enough to be easily read
86+
# by the viewer.
87+
plt.text(2011.5, y_pos, column, fontsize=14, color=color_sequence[rank])
88+
89+
# Make the title big enough so it spans the entire plot, but don't make it
90+
# so big that it requires two lines to show.
91+
92+
# Note that if the title is descriptive enough, it is unnecessary to include
93+
# axis labels; they are self-evident, in this plot's case.
94+
plt.title('Percentage of Bachelor\'s degrees conferred to women in '
95+
'the U.S.A. by major (1970-2011)\n', fontsize=18, ha='center')
96+
97+
# Finally, save the figure as a PNG.
98+
# You can also save it as a PDF, JPEG, etc.
99+
# Just change the file extension in this call.
100+
plt.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight')
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Year,Agriculture,Architecture,Art and Performance,Biology,Business,Communications and Journalism,Computer Science,Education,Engineering,English,Foreign Languages,Health Professions,Math and Statistics,Physical Sciences,Psychology,Public Administration,Social Sciences and History
2+
1970,4.22979798,11.92100539,59.7,29.08836297,9.064438975,35.3,13.6,74.53532758,0.8,65.57092343,73.8,77.1,38,13.8,44.4,68.4,36.8
3+
1971,5.452796685,12.00310559,59.9,29.39440285,9.503186594,35.5,13.6,74.14920369,1,64.55648516,73.9,75.5,39,14.9,46.2,65.5,36.2
4+
1972,7.42071022,13.21459351,60.4,29.81022105,10.5589621,36.6,14.9,73.55451996,1.2,63.6642632,74.6,76.9,40.2,14.8,47.6,62.6,36.1
5+
1973,9.653602412,14.7916134,60.2,31.14791477,12.80460152,38.4,16.4,73.50181443,1.6,62.94150212,74.9,77.4,40.9,16.5,50.4,64.3,36.4
6+
1974,14.07462346,17.44468758,61.9,32.99618284,16.20485038,40.5,18.9,73.33681143,2.2,62.41341209,75.3,77.9,41.8,18.2,52.6,66.1,37.3
7+
1975,18.33316153,19.13404767,60.9,34.44990213,19.68624931,41.5,19.8,72.80185448,3.2,61.64720641,75,78.9,40.7,19.1,54.5,63,37.7
8+
1976,22.25276005,21.39449143,61.3,36.07287146,23.4300375,44.3,23.9,72.16652471,4.5,62.14819377,74.4,79.2,41.5,20,56.9,65.6,39.2
9+
1977,24.6401766,23.74054054,62,38.33138629,27.16342715,46.9,25.7,72.45639481,6.8,62.72306675,74.3,80.5,41.1,21.3,59,69.3,40.5
10+
1978,27.14619175,25.84923973,62.5,40.11249564,30.52751868,49.9,28.1,73.19282134,8.4,63.61912216,74.3,81.9,41.6,22.5,61.3,71.5,41.8
11+
1979,29.63336549,27.77047744,63.2,42.06555109,33.62163381,52.3,30.2,73.82114234,9.4,65.08838972,74.2,82.3,42.3,23.7,63.3,73.3,43.6
12+
1980,30.75938956,28.08038075,63.4,43.99925716,36.76572529,54.7,32.5,74.98103152,10.3,65.28413007,74.1,83.5,42.8,24.6,65.1,74.6,44.2
13+
1981,31.31865519,29.84169408,63.3,45.24951206,39.26622984,56.4,34.8,75.84512345,11.6,65.83832154,73.9,84.1,43.2,25.7,66.9,74.7,44.6
14+
1982,32.63666364,34.81624758,63.1,45.96733794,41.94937335,58,36.3,75.84364914,12.4,65.84735212,72.7,84.4,44,27.3,67.5,76.8,44.6
15+
1983,31.6353471,35.82625735,62.4,46.71313451,43.54206966,58.6,37.1,75.95060123,13.1,65.91837999,71.8,84.6,44.3,27.6,67.9,76.1,44.1
16+
1984,31.09294748,35.45308311,62.1,47.66908276,45.12403027,59.1,36.8,75.86911601,13.5,65.74986233,72.1,85.1,46.2,28,68.2,75.9,44.1
17+
1985,31.3796588,36.13334795,61.8,47.9098841,45.747782,59,35.7,75.92343971,13.5,65.79819852,70.8,85.3,46.5,27.5,69,75,43.8
18+
1986,31.19871923,37.24022346,62.1,48.30067763,46.53291505,60,34.7,76.14301516,13.9,65.98256091,71.2,85.7,46.7,28.4,69,75.7,44
19+
1987,31.48642948,38.73067535,61.7,50.20987789,46.69046648,60.2,32.4,76.96309168,14,66.70603055,72,85.5,46.5,30.4,70.1,76.4,43.9
20+
1988,31.08508746,39.3989071,61.7,50.09981147,46.7648277,60.4,30.8,77.62766177,13.9,67.14449816,72.3,85.2,46.2,29.7,70.9,75.6,44.4
21+
1989,31.6124031,39.09653994,62,50.77471585,46.7815648,60.5,29.9,78.11191872,14.1,67.01707156,72.4,84.6,46.2,31.3,71.6,76,44.2
22+
1990,32.70344407,40.82404662,62.6,50.81809432,47.20085084,60.8,29.4,78.86685859,14.1,66.92190193,71.2,83.9,47.3,31.6,72.6,77.6,45.1
23+
1991,34.71183749,33.67988118,62.1,51.46880537,47.22432481,60.8,28.7,78.99124597,14,66.24147465,71.1,83.5,47,32.6,73.2,78.2,45.5
24+
1992,33.93165961,35.20235628,61,51.34974154,47.21939541,59.7,28.2,78.43518191,14.5,65.62245655,71,83,47.4,32.6,73.2,77.3,45.8
25+
1993,34.94683208,35.77715877,60.2,51.12484404,47.63933161,58.7,28.5,77.26731199,14.9,65.73095014,70,82.4,46.4,33.6,73.1,78,46.1
26+
1994,36.03267447,34.43353129,59.4,52.2462176,47.98392441,58.1,28.5,75.81493264,15.7,65.64197772,69.1,81.8,47,34.8,72.9,78.8,46.8
27+
1995,36.84480747,36.06321839,59.2,52.59940342,48.57318101,58.8,27.5,75.12525621,16.2,65.93694921,69.6,81.5,46.1,35.9,73,78.8,47.9
28+
1996,38.96977475,35.9264854,58.6,53.78988011,48.6473926,58.7,27.1,75.03519921,16.7,66.43777883,69.7,81.3,46.4,37.3,73.9,79.8,48.7
29+
1997,40.68568483,35.10193413,58.7,54.99946903,48.56105033,60,26.8,75.1637013,17,66.78635548,70,81.9,47,38.3,74.4,81,49.2
30+
1998,41.91240333,37.59854457,59.1,56.35124789,49.2585152,60,27,75.48616027,17.8,67.2554484,70.1,82.1,48.3,39.7,75.1,81.3,50.5
31+
1999,42.88720191,38.63152919,59.2,58.22882288,49.81020815,61.2,28.1,75.83816206,18.6,67.82022113,70.9,83.5,47.8,40.2,76.5,81.1,51.2
32+
2000,45.05776637,40.02358491,59.2,59.38985737,49.80361649,61.9,27.7,76.69214284,18.4,68.36599498,70.9,83.5,48.2,41,77.5,81.1,51.8
33+
2001,45.86601517,40.69028156,59.4,60.71233149,50.27514494,63,27.6,77.37522931,19,68.57852029,71.2,85.1,47,42.2,77.5,80.9,51.7
34+
2002,47.13465821,41.13295053,60.9,61.8951284,50.5523346,63.7,27,78.64424394,18.7,68.82995959,70.5,85.8,45.7,41.1,77.7,81.3,51.5
35+
2003,47.93518721,42.75854266,61.1,62.1694558,50.34559774,64.6,25.1,78.54494815,18.8,68.89448726,70.6,86.5,46,41.7,77.8,81.5,50.9
36+
2004,47.88714025,43.46649345,61.3,61.91458697,49.95089449,64.2,22.2,78.65074774,18.2,68.45473436,70.8,86.5,44.7,42.1,77.8,80.7,50.5
37+
2005,47.67275409,43.10036784,61.4,61.50098432,49.79185139,63.4,20.6,79.06712173,17.9,68.57122114,69.9,86,45.1,41.6,77.5,81.2,50
38+
2006,46.79029957,44.49933107,61.6,60.17284465,49.21091439,63,18.6,78.68630551,16.8,68.29759443,69.6,85.9,44.1,40.8,77.4,81.2,49.8
39+
2007,47.60502633,43.10045895,61.4,59.41199314,49.00045935,62.5,17.6,78.72141311,16.8,67.87492278,70.2,85.4,44.1,40.7,77.1,82.1,49.3
40+
2008,47.570834,42.71173041,60.7,59.30576517,48.88802678,62.4,17.8,79.19632674,16.5,67.59402834,70.2,85.2,43.3,40.7,77.2,81.7,49.4
41+
2009,48.66722357,43.34892051,61,58.48958333,48.84047414,62.8,18.1,79.5329087,16.8,67.96979204,69.3,85.1,43.3,40.7,77.1,82,49.4
42+
2010,48.73004227,42.06672091,61.3,59.01025521,48.75798769,62.5,17.6,79.61862451,17.2,67.92810557,69,85,43.1,40.2,77,81.7,49.3
43+
2011,50.03718193,42.7734375,61.2,58.7423969,48.18041792,62.2,18.2,79.43281184,17.5,68.42673015,69.5,84.8,43.1,40.1,76.7,81.9,49.2

0 commit comments

Comments
 (0)
0