How to create a histogram

Description

A histogram is a very common and useful data visualization. It displays an approximation of the distribution in single series of data points (one variable) by grouping the data into bins, each bin draw as a vertical bar. How can we create such a visualization?

Using Matplotlib, in Python

View this solution alone.

We will create some random data using NumPy, but that’s just for demonstration purposes. You can apply the answer below to any data, even if it’s stored just in plain Python lists.

1
2
import numpy as np
data = np.random.normal( size=1000 )


The conventional way to import matplotlib in Python is as follows.

1
import matplotlib.pyplot as plt


To create a histogram with 10 bins (the default):

1
2
plt.hist( data )  # or plt.hist( bins=20 ), or any number
plt.show()


The $y$ axis in a histogram is frequency, or number of occurrences. You can change it to probabilities instead.

1
2
plt.hist( data, density=True )
plt.show()


You can also choose your own bin boundaries.

1
2
plt.hist( data, bins=range(-10,10,1) )
plt.show()


See a problem? Tell us or edit the source.

Solution, in R

View this solution alone.

We will create some random data, but that’s just for demonstration purposes. You can apply the answer below to any data. Simply replace the data variable with your real data (a list, a column of a dataframe, etc.).

1
data <- rnorm(1000)


We can use R’s hist() function to create the histogram.

1
hist(data)


The y axis in a histogram is frequency, or the number of occurences. You can change it to probabilities instead.

1
hist(data, prob = TRUE)


You can also choose your own bin boundaries. You might specify the number of bin breaks you want, or you can choose the exact bin breaks that you want.

1
2
hist(data, breaks = 8)                # Specify number of bin breaks
hist(data, breaks = c(seq(-5, 5, 1))) # Choose exact bin breaks