Now we want to start talking about distribution plots and specifically the histogram, which is a type
of distribution plot.
So this is an example of a histogram we have here age across the horizontal axis and population in thousands
along the vertical axis.
So this value here 50 means 50,000, 100 is 100,000 etc. and the ages along the horizontal axis are
ages zero and up.
And then for the second part of the histogram here, ages ten and up, which means this first bar or
this first bin is ages 0 to 9.
This bin right here is ages 10 to 19, this bin is 20 to 29, etc. all the way up to this last bin,
which is 100 to 109.
This is a histogram that, for example, might represent the population of a city.
So maybe in this city, if we're trying to read the histogram, what it tells us is that there are about
25,000, zero, ten nine year olds living in the city, maybe about, let's say 40,000, 10 to 19 year
olds living in the city all the way up to the largest group, which is the 40 to 49 year olds, of which
it looks like there might be about 170,000.
Here are the things that we want to understand about a histogram.
First of all, this horizontal axis feature, whatever characteristic were plotting along the horizontal
axis, in this case, its age needs to be continuous.
So age is a continuous characteristic, time is continuous, height and weight are both continuous because
all of these things can be broken down into smaller and smaller and smaller measurements, whereas a
different kind of characteristic might be discrete.
So in the last lesson, we looked at line plots and we plotted months along the horizontal axis.
So January, February, March, April, etc. While months are similar to time, if we're specifically
talking about months, there's nothing in between January and February, there's nothing in between
February and March.
Those are distinct discrete buckets.
We're not talking about a continuous characteristic.
Compare that to something like this here with age where in this 0 to 9 years old bucket, we could have
someone who is exactly four years old, but we could also have someone who is four years, three months,
27 days, 4 hours, 6 minutes and 32.3 seconds years old, and they would fall into this bucket and
there's a clean cut off.
The moment that person turns ten years old, they would graduate from this first bucket into this second
bucket.
So this feature along the horizontal axis needs to be continuous if we're going to use a histogram to
represent the data.
Now, because of the continuity of the data set, we sketch a bar for each bin or class that we list
out here along the horizontal axis, and we include no gaps in between each bar in our plot and including
no gaps, suggests the continuous nature of this characteristic along the horizontal axis.
When we talk about bar plots or bar charts, which we'll look at later, they'll look a lot like histograms,
except that we will leave a little space or a little gap in between each bar here with the histogram,
we leave no space or no gap between the bars.
So right here or right here, there's no gap between these bars.
And that's because of the continuous nature of this characteristic along the horizontal axis.
Now, that being said, of course, like we've talked about, each bar is a count of the number of occurrences
that fall into this bin or class or interval.
So the height of each bar represents the number of occurrences that we find of people ages 0 to 9 in
this city.
This bar here, the height of it, represents the number of occurrences of people that we find between
ages ten and 19 in this city.
Now, one of the reasons that a histogram is really helpful is because it simplifies what could otherwise
be a very large and messy data set here.
We can clearly tell the population of this city is very large.
It's multiple hundreds of thousands.
If we were to try to plot a value along our chart here for the age of every single person in the city,
the chart would quickly get very out of control because again, we would need a bar for a person of
each age.
And so if we had two people whose ages were different by even one day or even a couple of hours or a
second, that would be a different individual bar on our chart.
It would be a different individual age or timestamp along our horizontal axis that would get extremely
overwhelming.
It would make our chart so messy that it would be unreasonable to interpret it.
What makes a lot more sense is that when we have several hundreds of thousands of people, we group
them into bins.
And then create this histogram that gives us a much clearer, simpler picture of the population when
we're creating a histogram.
We want to make sure that our buckets or bins or classes, whatever we're using here along the horizontal
axis that that bin size, that that class width is always the same.
So we have here for this example, ten year increments, we have ages 0 to 9, 10 to 19, 22, 29,
32, 39, 42, 49, all the way up to 100 to 109.
Keeping that bin width or that class width.
The same is the only way that the histogram makes sense.
If we instead have one bin for ages 0 to 9, another one for 10 to 19 and that have been for 20 to 49
and then maybe 50 to 59 and then 60 all the way up to 109.
It's going to make the data almost meaningless because those bin widths aren't the same.
So we can't interpret or compare different bins to get an accurate picture of what our age distribution
is looking like.
So we need to make sure that our bin width, that our class width is always equivalent so that this
distribution is consistent across each bucket along the horizontal axis.
So we know that we have to keep bin with the same.
But how do we know how many bins to use based on our data?
Well, that's a little bit more of an art than it is a perfect science.
But again, as we've said before, with all of these kinds of charts and plots, we're just trying to
communicate a clear picture of the data.
So we need to use whatever number of bins will communicate the idea we're trying to convey.
So for instance, with this age data, for these age intervals across our city's population, we can
see that we have people from ages zero all the way to 109.
So we could certainly break that data into two buckets, classifying people into either the 0 to 54
group or the 55 to 109 group and just have literally two bins or two classes.
And while that's not wrong, it certainly wouldn't give us as clear of a picture as this histogram.
This gives us much more information about the age distribution of our population.
So with an age range like this, we probably wouldn't want to use two buckets, but we also wouldn't
want to use, let's say 55 buckets because that would break this up into only two year bins or two year
increments and that would maybe be more detailed than we need.
So it's about finding something reasonable in the middle.
It gives us a good picture of the distribution without giving us more detail than we really need.
And hopefully you have a good gut feel for if our age range is essentially 0 to 110, then breaking
up that interval into ten year buckets gives us 11 bars in our distribution.
That's probably pretty reasonable.
An 11 bar distribution should give us a pretty good picture without having buckets that are too big,
that we don't get enough data or too small, that we get too much data.
So we're just looking for a good middle ground and anything within that middle ground should give us
a decent histogram.
If you build a histogram and you feel like the bin width was too wide or too narrow, you can always
make an adjustment to get a less detailed distribution or a more detailed distribution.
Now some of the simple math that goes along with this, let's say that we wanted to build a histogram
from the raw data of the ages of the people who live in this city.
We want to think about the smallest value in the dataset and the largest value in the dataset.
So in this case, the smallest value is age zero.
The largest value is age 109.
So to get the entire interval, because we're including both zero and 109, we have to to find the interval,
say 109 -0, the largest value minus the smallest value.
But if we're including both of those values in the dataset, both of those endpoints, then the subtraction
is what's called inclusive and we have to add one to get the full range of the data.
In other words, if you were to use your fingers to count starting at zero, so counting on your fingers,
you say zero one, two, three, four, all the way up to 109.
You would actually count on your fingers to.
110 because you're including that zero value and that 109 value.
That's why we add that plus one.
So we have this range of 110 values.
So then we just need to pick whatever seems to be a reasonable class interval or class width or bin
width.
And let's say that we want to divide this up into every ten years like we did.
So if we divide this by ten, then that means that we will have 11 different buckets or 11 different
classes, each with a width of ten.
So then we take our smallest value, which is zero.
We know the interval is ten.
So if we again count on our fingers from zero, so 012 all the way up until we get to our 10th finger,
then the upper bound of that first interval is nine.
So we know that our first bin, our first class is ages 0 to 9, and then from there we can find the
rest of our classes ages 10 to 19, ages 20 to 29.
And notice that the difference between all of the class lower bounds will always be the bin width.
So the difference between zero and ten, ten and 20 is always ten.
And the difference between the upper bounds of all of our intervals will also be that same bin width.
So the difference between nine and 19, between 19 and 29 is always ten.
So whatever width we find for each class or each bin, we should see reflected here between the lower
bounds and here between the upper bounds.
And if we keep going here 30 to 39, all the way down to our highest value, we get to 102, 109.
If we then count the number of bins that we have, we should have 11 bins since we calculated that bin
count earlier, so 11 bins each with a width of ten.
Then once we have the range of each bin, we can in our raw data, count the number of occurrences that
fall within that bin.
So if we have our raw data, we would count up the number of people in our data ages 0 to 9, and we
would record that value.
So let's say that's 25,000 people.
We would count up the number of people who have an age between ten and 19.
And let's say based on our graph here, that that's about.
40,000 people.
So we would count the number of occurrences in each bin or in each class.
And then to sketch the histogram, we just place each class, each bin along the horizontal axis in
order, and then we place counts here along our vertical axis and we plot the height of each bar in
the histogram according to the number of occurrences that we see in each bin or each class.
And that is how we build a histogram from a raw data set.
That's the basic idea behind a histogram, which is our first type here of distribution plot.
These are going to become more and more important as we go forward because what we realize is that this
distribution can eventually create for us a probability distribution if we connect the top of each bar
in the histogram, if we find the midpoint of each class and then we sketch a point at the top of each
bar here.
And if we eventually connect these points with a smooth curve, what we get is a distribution that models
our raw data and eventually we can use that distribution to answer probability questions about how likely
it is, for instance, that choosing a person at random in our city will give us a person between the
ages of 40 and 49 or between the ages of 70 and 79.
So not only is the histogram an important plot or chart like the scatter plots and line plots that we've
looked at so far and like the charts and graphs that we'll look at next.
But this kind of distribution plot will also be important for us to remember as we move forward to learn
more about probability and statistics.