]> projects.mako.cc - matplotlib-cdsw/blob - hist2d.py
Add another, more complicated plotting example.
[matplotlib-cdsw] / hist2d.py
1 """Make a 2D histogram ("heat map") of the male and female articles by birth year and first edit date"""
2 import wikibios
3 from datetime import datetime
4 from matplotlib import pyplot, dates
5
6 figure = pyplot.figure()
7
8 min_firstedit = min(dates.date2num(wikibios.columns['firstedit']))
9 max_firstedit = max(dates.date2num(wikibios.columns['firstedit']))
10
11 firstedits_male = dates.date2num(wikibios.columns_male['firstedit'])
12 birth_years_male = wikibios.columns_male['birth_year']
13
14 axes1 = figure.add_subplot(1,2,1)
15 h1 = axes1.hist2d(firstedits_male, birth_years_male, range=[[min_firstedit, max_firstedit], [1750, 2014]], bins=50)
16 figure.colorbar(h1[3])
17 axes1.xaxis.set_major_formatter(dates.AutoDateFormatter(dates.AutoDateLocator()))
18 axes1.set_title('Male')
19
20 firstedits_female = dates.date2num(wikibios.columns_female['firstedit'])
21 birth_years_female = wikibios.columns_female['birth_year']
22
23 axes2 = figure.add_subplot(1,2,2)
24 h2 = axes2.hist2d(firstedits_female, birth_years_female, range=[[min_firstedit, max_firstedit], [1750, 2014]], bins=50)
25 figure.colorbar(h2[3])
26 axes2.xaxis.set_major_formatter(dates.AutoDateFormatter(dates.AutoDateLocator()))
27 axes2.set_title('Female')
28
29 figure.savefig('hist2d.png')

Benjamin Mako Hill || Want to submit a patch?