Skip to content
Start free

How do I draw and read a histogram of a numeric column?

Draw trip duration as a histogram, adjust the bins and read the shape: where it piles up, how long the tail is and what sits far away.

Free course. Your Maestro, an AI, .

You already know what a row of Rueda is, what type each column has and where data is missing. In this module you look inside each column, starting with the one that will answer the most questions: duracion_min, how long each trip lasts.

An average or a maximum gives you one number. A histogram gives you the shape. It splits the column into bins of equal width (0 to 5 minutes, 5 to 10, 10 to 15...) and draws one bar per bin, as tall as the number of trips that fall in it. The height is not the length of a trip: it's how many trips are in that bin.

With that shape you answer three questions you'll ask of any numeric column:

  • Where does it pile up? The bins with the tallest bars are what's common.
  • Which way does it stretch? If the bars taper off slowly toward one side, the column has a tail. A tail on the right means a few very large values.
  • What sits far away? Lone bars, cut off from the rest by empty bins, are values you'll investigate.

Before you draw it, place a bet. Here are two numbers up front: the shortest trip lasts 0.3 minutes and the longest 1,452.6.

Check yourself: Without seeing the chart: what shape do you think Rueda's trip durations have?

Now draw it. In Altair, bin=True asks the chart to split the axis into bins and pick their width; according to the Vega-Lite documentation, maxbins is 10 by default, so it uses up to 10 bins. count() counts the trips in each bin.

You'll run into a problem that's common with columns like this one: almost everything lands in a single bar. That doesn't mean every trip lasts about the same. Three trips of more than 1,300 minutes stretch the axis out to 1,600, and with 200-minute bins the other 300 trips all fit in the first one. To see the shape of what's common, look separately at the range where almost everything lives.

The chart is a histogram of the whole column, and almost everything lands in one bar. Add .transformfilter("datum.duracionmin < 120") at the end to look only at trips under 120 minutes, and change bin=True to bin=alt.Bin(maxbins=40) for finer bins. You're done when you can see wh

Your turn

The chart is a histogram of the whole column, and almost everything lands in one bar. Add .transform_filter("datum.duracion_min < 120") at the end to look only at trips under 120 minutes, and change bin=True to bin=alt.Bin(maxbins=40) for finer bins. You're done when you can see where the trips pile up and which way the tail stretches.

The chart redraws when you pause typing, or right away with Run.

Ready to run your code.

What the chart shows

Histogram of the duration of the 303 trips that have one, in 200-minute bins: 300 trips fall in the first bin, 2 between 1,200 and 1,400 minutes and 1 between 1,400 and 1,600.

Data: viajes-rueda, 311 rows

Enter saves a cell and Escape leaves it as it was. Leave a cell empty for a missing value.

The exercise's data, "viajes-rueda". Your edits run with your code.
RowRemove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Page 1 of 16

Run it yourself

With the filter and the finer bins, the shape appears. Read it from left to right.

There are almost no trips under 5 minutes. The pile is between 10 and 30 minutes: 228 of the 300 trips under 120 minutes fall there. Then the bars drop off and thin out: 8 trips go past 45 minutes and just one goes past 60. That shape, with the pile on the left and the tail to the right, is called right-skewed. It's the typical shape of durations, amounts and wait times: there's a floor (nothing lasts less than zero) and no ceiling.

Two cautions. The < 120 filter is for looking, not for cleaning: the three very long trips are still in the table and in your log, and on the error-or-real page you decide what they are. And the number of bins changes what you see: with too few, the shape hides; with too many, each bar holds one or two trips and the chart looks like noise. Try changing maxbins to 10 and to 100 and compare.

The chart below shows the same trips another way: one tick per trip, split by user type. Step through it.

Use the left and right arrow keys to change steps.

Step 1 of 5

The whole chart

Each tick is a trip under 120 minutes, in the row for its user type. Where the ticks overlap so much they look like a smudge, there are many trips.

Try it on another table

A column's shape is the first thing you write down about it. In your log, for duracion_min, it could read: pile between 10 and 30 minutes, tail to the right up to 96; three trips over 1,300 and six under 1, to review. If it's hard to put the shape into words, ask your Maestro to help you describe it.

Now the same situation with other data.

Check yourself: A pharmacy draws a histogram of the totals on its 2,000 receipts from March. Almost all of them fall in the first bar, from $0 to $5,000, and there's a single short bar out near $48,000. What do you do first?