# 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?

Related tasks:

- How to create basic plots
- How to create a box (and whisker) plot
- How to add details to a plot
- How to create bivariate plots to compare groups
- How to plot interaction effects of treatments

## Using Matplotlib, in Python

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()

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

## Solution, in R

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

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

## Topics that include this task

## Opportunities

This website does not yet contain a solution for this task in any of the following software packages.

- Excel
- Julia

If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.