Histograms

Learn how to draw histograms for data visualization in D3.

What are histograms

A histogram is a graphical representation of the distribution of numerical data. It is typically formed by creating “bins” of a larger dataset that group the data into a range of values and count the number of pieces of data that fall into each bin. Each bin is then represented as a bar showing the relationship between each range.

The example we will work through shows the frequency of earthquakes above magnitude 3 between July 2010 and January 2012 in Christchurch, New Zealand (a time of some significant seismic activity). Data was sourced from New Zealand’s Geonet site.

We can see that the data has been “binned” by month and that between the 1st of September and the 1st of October there were over 1,800 earthquakes registering over magnitude 3.

The data

The data for this example will be sourced from a CSV file named earthquakes.csv. It consists of a column of dates in day-month-year format (and magnitudes, which won’t be used in this graph), and its contents look similar to the following:

dtg,value
01-08-2010,3
01-08-2010,3
...

The code

The ...