Skip to content

Lesson 8 ยท One Variable

Distributions and Histograms

Read the shape of a variable with histograms, box plots and ECDF charts: spot skew, multiple peaks, gaps and heaping, and see how bin width changes it.

Summary numbers compress a column into a few values. A distribution shows the whole picture: which values occur and how often. Two columns with the same mean and standard deviation can have completely different shapes, so always look.

Histograms

A histogram splits the number line into bins and counts the values in each one. You can make a text histogram with plain Python, which shows exactly what happens. Each # below stands for about two trips.

Python A text histogram

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

There are two peaks. This is a bimodal distribution, and it often suggests two different kinds of things mixed together: perhaps short city trips and longer airport trips. The overall mean would fall in the valley between them, where few trips happen.

Now change the last line to histogram(trips, 20). The two peaks are much harder to see in such wide bins.

Choose bins with care

Bins that are too wide hide structure. Bins that are too narrow create noise. Try several widths and see which features stay. A feature that appears with only one setting deserves doubt.

Python Same data, three bin settings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The Matplotlib histogram lesson covers more options.

Words for shapes

Shapes to notice

Symmetric Similar tails
Right skew Long tail of large values
Two peaks Maybe mixed groups
Hard edge Limits or filters
1 / 2
  • Right-skewed data has a long tail toward large values. Incomes, order values and response times often look like this.
  • Left-skewed data has the long tail toward small values, as with scores on an easy test.
  • Gaps can show missing ranges or separate groups.
  • Spikes at one value can be real (a popular price) or artificial (a default).
  • Truncation is a sharp edge, for example when only orders above a minimum are stored.

Look for heaping

People round numbers, and systems cap them. Counting exact values can reveal both.

Python Find suspicious spikes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The heavy share of round ages suggests estimates, and the repeated 99 may be a code for "unknown" or a form limit. Confirm before you act. The 20% figure is only a rough baseline for ages spread fairly evenly across last digits.

Box plots

A box plot draws the median as a line, the middle half as a box and "whiskers" toward the rest. In Matplotlib's default, each whisker reaches the furthest data point within 1.5 times the IQR from the box; points beyond that are drawn one by one.

A box plot is compact and great for comparing many groups. Its weakness is that it hides peaks: a two-peak distribution can produce an ordinary-looking box.

Python Box plot and histogram side by side

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

(Newer Matplotlib versions prefer orientation="horizontal" to vert=False. Use the form that your installed version accepts without a warning.)

The ECDF

The empirical cumulative distribution function (ECDF) shows, for each value x, the share of data at or below x. It needs no bins and every observation counts. A steep part means many values are close together. A flat part means a gap.

Python Read shares from an ECDF

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

An ECDF answers questions such as "what share of deliveries arrive within 15 minutes?" directly. You can plot two groups on the same axes to compare their whole distributions.

Do not assume that data is bell-shaped. Shortcuts such as "about 95% of values lie within two standard deviations" rely on a roughly normal shape. Check the shape before you use them.

From shape to question

A shape is rarely the end of the story. Use it to ask what produced it:

What you see Questions to ask
Two peaks Which column separates the two groups?
A spike at zero Is zero a real value, a default or a missing marker?
A sharp upper edge Is there a cap in the form, sensor or system?
A long right tail Are the largest cases a different kind of customer?
A gap in the middle Did collection stop, or do two units get mixed?

When you suspect two groups, draw one histogram for each value of a candidate column, using the same bins and the same x-axis. If each group has a single peak, you have probably found a useful explanation. If both groups still have two peaks, keep looking.

Also think about group sizes. When one group is much larger, its bars can hide the smaller one. Plotting shares (a density) in place of counts makes shapes comparable, but then say so, and still report how many rows each group has.

Try it yourself

In the box plot example, remove the 60 longer trips and run it again. Compare both pictures. Then add them back with a center of 16 minutes in place of 28. At what point do the two peaks stop being visible as separate hills?

Compare with what you expect

Before you plot, sketch the shape you expect. Waiting times usually cannot be negative and often have a long right tail. Scores on a test have a lowest and highest possible value. If the picture contradicts what must be true, such as bars below zero for a duration, you have found a data problem. If it contradicts only what you assumed, you have found something to learn about the process that produced the data.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A histogram has two clear peaks. What is a sensible next step?

    Two peaks often point to different subpopulations, which you can try to separate.
  2. 2. What happens when bins are far too wide?

    Wide bins merge distinct features together.
  3. 3. What is one weakness of a box plot?

    A box is based on a few quantiles, so much of the internal shape is lost.
  4. 4. At x = 15, an ECDF has the value 0.7. What does this mean?

    The ECDF gives the share of observations at or below x.

Summary

  • A distribution shows what summary numbers hide.
  • Try several bin widths and trust features that persist.
  • Describe skew, peaks, gaps, spikes and hard edges, then look for causes.
  • Box plots compare groups compactly; histograms and ECDFs show more detail.
  • Do not assume a normal shape.

Next: Exploring Categorical Variables

Your progress is saved in this browser. No account needed.