annotate talk/code/plots.py @ 45:6daaf6a8e431

Add code to plot boys and girls
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sat, 14 May 2016 21:24:47 -0400
parents
children 7887a0b32539
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
1 import matplotlib.pyplot as plt
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
2 import matplotlib.cbook as cbook
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 import numpy as np
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
4
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
5 with open("../../data/men") as f:
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6 men = [float(x) for x in f.readlines()]
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 with open("../../data/women") as f:
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9 women = [float(x) for x in f.readlines()]
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 men_stats = cbook.boxplot_stats(men)
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 women_stats = cbook.boxplot_stats(women)
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 fig = plt.figure(figsize=(6,4))
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 # setup the figure and axes
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 bpAx = fig.add_axes([0.05, 0.7, 0.4, 0.2]) # left, bottom, width, height:
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 # (adjust as necessary)
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 histAx = fig.add_axes([0.05, 0.2, 0.4, 0.5]) # left specs should match and
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 # bottom + height on this line should
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21 # equal bottom on bpAx line
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22 # plot stuff
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 bpAx.bxp(men_stats, vert=False, flierprops={"marker": 'x'})
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24 histAx.hist(men, bins=100, color=[0.3, 0.3, 1])
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 # confirm that the axes line up
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 xlims = np.array([bpAx.get_xlim(), histAx.get_xlim()])
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 for ax in [bpAx, histAx]:
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
29 ax.set_xlim([xlims.min(), xlims.max()])
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
30
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
31 bpAx.set_xticklabels([]) # clear out overlapping xlabels
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
32 bpAx.set_yticks([]) # don't need that 1 tick mark
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
33 bpAx.set_title("Ages of actors")
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
34
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
35
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
36
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
37 # setup the figure and axes
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
38 bpAx = fig.add_axes([0.55, 0.7, 0.4, 0.2]) # left, bottom, width, height:
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
39 # (adjust as necessary)
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
40 histAx = fig.add_axes([0.55, 0.2, 0.4, 0.5]) # left specs should match and
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
41 # bottom + height on this line should
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
42 # equal bottom on bpAx line
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
43 # plot stuff
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
44 bpAx.bxp(women_stats, vert=False, flierprops={"marker": 'x'})
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
45 histAx.hist(women, bins=100, color='pink')
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
46
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
47 # confirm that the axes line up
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
48 xlims = np.array([bpAx.get_xlim(), histAx.get_xlim()])
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
49 for ax in [bpAx, histAx]:
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
50 ax.set_xlim([xlims.min(), xlims.max()])
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
51
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
52 bpAx.set_xticklabels([]) # clear out overlapping xlabels
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
53 bpAx.set_yticks([]) # don't need that 1 tick mark
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
54 bpAx.set_title("Ages of actresses")
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
55
6daaf6a8e431 Add code to plot boys and girls
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
56 plt.show()