]> projects.mako.cc - matplotlib-cdsw/blob - fractionplot.py
scatterplot: changed linewidth to edgecolor
[matplotlib-cdsw] / fractionplot.py
1 """Plot the fraction of articles that are female, by birth year and first edit date."""
2 import wikibios
3 from matplotlib import pyplot
4 from operator import itemgetter
5
6 figure = pyplot.figure()
7
8 rows_by_birth_year = sorted(wikibios.rows, key=itemgetter('birth_year'))
9 birth_year_medians = []
10 fraction_female_by_birth_year = []
11 N = 1000
12 i = 0
13 while i + N <= len(rows_by_birth_year):
14         chunk = rows_by_birth_year[i:i+N]
15         i = i + N
16
17         birth_year_medians.append(chunk[N / 2]['birth_year'])
18
19         count_female = 0.0
20         for row in chunk:
21                 if row['gender'] == 'female':
22                         count_female = count_female + 1
23         fraction_female = count_female / N
24         fraction_female_by_birth_year.append(fraction_female)
25
26 axes1 = figure.add_subplot(2, 1, 1)
27 axes1.plot(birth_year_medians, fraction_female_by_birth_year)
28 axes1.set_xlabel('Birth Year')
29 axes1.set_ylabel('Fraction Female')
30
31 rows_by_firstedit = sorted(wikibios.rows, key=itemgetter('firstedit'))
32 fraction_female_by_firstedit = []
33 firstedit_medians = []
34 i = 0
35 while i + N <= len(rows_by_birth_year):
36         chunk = rows_by_firstedit[i:i+N]
37         i = i + N
38
39         firstedit_medians.append(chunk[N / 2]['firstedit'])
40
41         count_female = 0.0
42         for row in chunk:
43                 if row['gender'] == 'female':
44                         count_female = count_female + 1
45         fraction_female = count_female / N
46         fraction_female_by_firstedit.append(fraction_female)
47
48 axes2 = figure.add_subplot(2, 1, 2)
49 axes2.plot(firstedit_medians, fraction_female_by_firstedit)
50 axes2.set_xlabel('Article Year')
51 axes2.set_ylabel('Fraction Female')
52
53 figure.savefig('fractions.pdf')

Benjamin Mako Hill || Want to submit a patch?