Histograms#

from datascience import *
from cs104 import *
import numpy as np

%matplotlib inline

0. Error practice#

majors = Table().read_table("data/majors.csv")
majors.show(5)
Major Division 2008-2012 2022-2025
Africana Studies 2 0 2
American Studies 2 10 12
Anthropology 2 8 4
Arabic Studies 1 4 4
Art 1 55 38

... (35 rows omitted)

# Select only division 3 majors 
div3 = majors.where("Division", are.equal_to(3)
div3
  Cell In[4], line 2
    div3 = majors.where("Division", are.equal_to(3)
                       ^
SyntaxError: '(' was never closed
# Get the number of majors across both time periods
majors.select("2008-2012") + majors.select("2022-2025")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 2
      1 # Get the number of majors across both time periods
----> 2 majors.select("2008-2012") + majors.select("2022-2025")

TypeError: unsupported operand type(s) for +: 'Table' and 'Table'

1. Overlaid graphs#

Sometimes we want to see more than one plot on a single graph.

Overlaid bar charts#

div3 = majors.where("Division", are.equal_to(3)).drop("Division")
div3
Major 2008-2012 2022-2025
Astronomy 1 0
Astrophysics 3 4
Biology 58 52
Chemistry 30 30
Computer Science 16 59
Environmental Science 0 0
Geosciences 7 12
Mathematics 53 62
Physics 12 16
Psychology 62 62

... (1 rows omitted)

# First graph for 2008-2012
div3.barh("Major", "2008-2012")
# Second graph for 2022-2025
div3.barh("Major", "2022-2025")
../_images/08-histograms_11_0.png ../_images/08-histograms_11_1.png

Overlaid graph puts the two graphs together to make comparison easier.

The package we’re using will automatically make overlaid graphs with the remainder of the columns if you give it just one parameter.

div3.barh("Major")
../_images/08-histograms_13_0.png

Overlaid line plots#

temps_by_month = Table().read_table("data/temps_by_month_upernavik.csv")
temps_by_month.show(5)
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1875 -15.6 -19.7 -25.9 -14.7 -9.6 -0.4 4.7 2.9 -0.1 -4.5 -5.5 -14
1876 -24.5 -21.2 -20.8 -14.9 -6.3 0.9 3.9 2.4 3.2 -6.2 -9.4 -14.4
1877 -21.1 -26.5 -17.8 -12 -1.7 1.4 4.6 5.2 3 -2.8 -10 -19.2
1878 -22.9 -26.9 -19.6 -13 -5.6 1.9 3.2 4.3 -0.9 -4 -2 -3.7
1879 -13.5 -25.4 -21.3 -13 -2.7 0 5.2 5.8 -1.1 -5.5 -8.2 -16.5

... (104 rows omitted)

As with bar charts, if you supply only one parameter, the plot method will plot a line for every other column.

temps_by_month.plot("Year")
../_images/08-histograms_17_0.png

Qualitatively, we can see that the plot above has too much information on it which makes it not very useful for understand trends.

temps_by_month.select("Year", "Feb", "Aug").plot("Year")
../_images/08-histograms_19_0.png

Overlaid scatter plots#

  • We want to plot points (the values of two numerical variables) from different groups on the same graph.

  • A new approach. Use categorical variable to break the rows into groups of related points in the plot.

finch_1975 = Table().read_table("data/finch_beaks_1975.csv")
finch_1975.show(6)
species Beak length, mm Beak depth, mm
fortis 9.4 8
fortis 9.2 8.3
scandens 13.9 8.4
scandens 14 8.8
scandens 12.9 8.4
fortis 9.5 7.5

... (400 rows omitted)

finch_1975.scatter("Beak length, mm", "Beak depth, mm", group="species")
../_images/08-histograms_22_0.png

Takeaway: The overlaid scatter plot above helps us very quickly discern differences between groups. In this case, we can quickly tell that the two Finch species have evolved (via natural selection) to have different beak characteristics.

2. Histograms#

A Histogram shows us the distribution of a numerical variable.

Midterm scores#

scores = Table().read_table("data/scores_by_section.csv")
scores = scores.relabeled("Midterm", "Midterm 1")
scores
Section Midterm 1
1 22
2 12
2 23
2 14
1 20
3 20
4 19
1 24
1 0
1 13

... (111 rows omitted)

Let’s subset to just section 4 for now.

scores_sec4 = scores.where("Section", 4)
scores_sec4
Section Midterm 1
4 19
4 14
4 24
4 12
4 0
4 13
4 19
4 22
4 13
4 18

... (20 rows omitted)

A histogram can give us a sense of the data as a whole: What are the common values? What are uncommon? How much variability is there? What are the extremes?

scores_sec4.hist("Midterm 1")
../_images/08-histograms_30_0.png

Class survey: Distance from home#

Load Data#

survey = Table().read_table("data/prelab01-survey-fall2026.csv")
survey.show(5)
Favorite icecream flavor Favorite planet Height (in inches) Distance Home (in miles) Birthday month Left or right handed?
Chocolate Mars 74 136 March Right handed
Strawberry Venus 65 168 January Right handed
Purple Cow Pluto* because liking Pluto is a protest move against Pl ... 63 2584 May Right handed
Purple Cow Venus 66 166 October Right handed
Purple Cow Neptune 66.9291 6751 July Right handed

... (27 rows omitted)

survey.labels
('Favorite icecream flavor ',
 'Favorite planet',
 'Height (in inches)',
 'Distance Home (in miles)',
 'Birthday month',
 'Left or right handed? ')
distance_home = survey.column('Distance Home (in miles)')
distance_home
array([ 136. ,  168. , 2584. ,  166. , 6751. ,  169. , 2953. ,  163. ,
         59. , 1246. , 2648. , 2857. ,  164. ,  128. ,   27.8,  169. ,
        269. , 1451. , 1446. ,  185. ,  852. ,  171. ,  183. ,  725. ,
        159. , 2473. ,  159. ,  192. ,  115. ,  235. ,  741. ,  173. ])

Some basic info about the distances:

len(distance_home)
32
np.mean(distance_home)
934.93125
max(distance_home)
6751.0

Sneak preview of a histogram for those distances

survey.hist('Distance Home (in miles)', bins=np.arange(0, 12000, 2000))
../_images/08-histograms_41_0.png

3. Binning#

survey.show(3)
Favorite icecream flavor Favorite planet Height (in inches) Distance Home (in miles) Birthday month Left or right handed?
Chocolate Mars 74 136 March Right handed
Strawberry Venus 65 168 January Right handed
Purple Cow Pluto* because liking Pluto is a protest move against Pl ... 63 2584 May Right handed

... (29 rows omitted)

We have a method in our package that can make bins automatically: table.bin.

our_range = np.arange(0,12000,2000)
our_range
array([    0,  2000,  4000,  6000,  8000, 10000])
binned_distance_home = survey.bin('Distance Home (in miles)', bins=our_range)
binned_distance_home
bin Distance Home (in miles) count
0 26
2000 5
4000 0
6000 1
8000 0
10000 0

Let’s add a column that is the percentage in each bin.

percent = binned_distance_home.column('Distance Home (in miles) count') / survey.num_rows * 100
percent_table = binned_distance_home.with_columns('Percent', percent)
percent_table
bin Distance Home (in miles) count Percent
0 26 81.25
2000 5 15.625
4000 0 0
6000 1 3.125
8000 0 0
10000 0 0

Histogram of distances from home#

survey.hist('Distance Home (in miles)', bins= np.arange(0,12000,2000))
../_images/08-histograms_50_0.png

Think-pair-share: Calculate the area of each bar in the histogram (estimating the height). Then show the sum of the area of all the bars equals 100.

#Possible approximations
widths = make_array(2000, 2000, 2000, 2000, 2000)
heights = make_array(0.041, 0.008, 0, 0.0015, 0)
areas = widths*heights
areas
array([82., 16.,  0.,  3.,  0.])
sum(areas)
101.0

Let’s check our estimates.

percent_table
bin Distance Home (in miles) count Percent
0 26 81.25
2000 5 15.625
4000 0 0
6000 1 3.125
8000 0 0
10000 0 0

Cool! We’re pretty close to the actual areas! Great!

Let’s work backwards now and see how our hist() method calculated the y-axis.

  1. Let’s look at the first bar/bin.

bin0 = percent_table.take(0)
bin0
bin Distance Home (in miles) count Percent
0 26 81.25

Recall, the height is equal to the (percent of entries in the bin) / width of the bin

percent_in_bin0 =  bin0.column('Percent').item(0)
percent_in_bin0
81.25
height0 = percent_in_bin0/2000
height0
0.040625

Fantastic! That’s what we see on the y-axis on the histogram.

More histogram practice#

survey.show(5)
Favorite icecream flavor Favorite planet Height (in inches) Distance Home (in miles) Birthday month Left or right handed?
Chocolate Mars 74 136 March Right handed
Strawberry Venus 65 168 January Right handed
Purple Cow Pluto* because liking Pluto is a protest move against Pl ... 63 2584 May Right handed
Purple Cow Venus 66 166 October Right handed
Purple Cow Neptune 66.9291 6751 July Right handed

... (27 rows omitted)

survey.labels
('Favorite icecream flavor ',
 'Favorite planet',
 'Height (in inches)',
 'Distance Home (in miles)',
 'Birthday month',
 'Left or right handed? ')
plot = survey.hist('Height (in inches)', bins=np.arange(58,82,2))
plot.set_ylim(0,0.15)
plot.set_title("Students in CS 104")
../_images/08-histograms_67_0.png

Think-pair-share: Approximating Histogram of heights#

  1. Approximate the percentage of the class that has height greater than or equal to 70 inches but less than 72 inches.

  2. How many students is this? We know 32 students responded to our survey.

survey.num_rows
32
answer_q1 = 11 * 2
answer_q1
22
answer_q2 = answer_q1 / 100 * 32
answer_q2
7.04

We can’t have a fraction of a student, but our approximation is very close. The real count is 7.

survey.where('Height (in inches)', are.between(70, 72)).num_rows
7

Overlaid histograms#

Scores#

Circle back around to our midterm data.

scores
Section Midterm 1
1 22
2 12
2 23
2 14
1 20
3 20
4 19
1 24
1 0
1 13

... (111 rows omitted)

scores_sec4 = scores.where("Section", 4)
scores_sec4.hist("Midterm 1")
../_images/08-histograms_77_0.png

Like scatter we can create overlaid histograms with the group= named variable

scores.hist("Midterm 1", group="Section", bins=10)
../_images/08-histograms_79_0.png

Finches#

One more overlay, for the two finch species.

finch_1975.show(10)
species Beak length, mm Beak depth, mm
fortis 9.4 8
fortis 9.2 8.3
scandens 13.9 8.4
scandens 14 8.8
scandens 12.9 8.4
fortis 9.5 7.5
fortis 9.5 8
fortis 11.5 9.9
fortis 11.1 8.6
fortis 9.9 8.4

... (396 rows omitted)

finch_1975.hist("Beak length, mm", bins=20)
../_images/08-histograms_82_0.png
plot = finch_1975.hist("Beak length, mm", group="species")
plot.set_title("Finches, 1975")
../_images/08-histograms_83_0.png

Try different bins to see differences in granularity.

def hist_with_bins(bins):
    finch_1975.hist("Beak length, mm", group="species", legend=False, bins=bins,  title=str(bins) + " bins")

interact(hist_with_bins, bins=Slider(1,20))

A few different histograms side-by-side:

with Figure(2,2, figsize=(4,3)):
    finch_1975.hist("Beak length, mm", group="species", legend=False, bins=6,  title="6 bins")
    finch_1975.hist("Beak length, mm", group="species", legend=False, bins=10, title="10 bins")    
    finch_1975.hist("Beak length, mm", group="species", legend=False, bins=15, title="16 bins")
    finch_1975.hist("Beak length, mm", group="species", legend=False, bins=20, title="20 bins")
../_images/08-histograms_88_0.png