Histograms with Matplotlib

Learn to build informative histograms in Python with Matplotlib.

Histograms with Matplotlib
Plotting histograms in Python with Matplotlib

Topics Covered

  1. What is a Histogram?
  2. Syntax of hist function in Matplotlib
  3. Plot a simple histogram in Matplotlib
  4. Plotting multiple datasets with different color in a histogram


What is a Histogram?

A histogram is a bar chart showing the frequency distribution of numerical data.

However, it is different from a bar chart. A histogram is used for continuous data, where the bins represent different ranges of data. A bar chart on the other hand, represents categorical variables.

You can use the hist() function to plot histograms in Matplotlib.


Syntax of hist function in Matplotlib

matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’, rwidth=None)

Parameters

  • x - Input values that can be a single array or a sequence of arrays.
  • bins - Range of values which can be either integer or sequence, default is 10.
  • range - The lower and upper range of the bins.
  • weights - This parameter denotes the weight of each value.
  • cumulative - The count of each bin together with the count of the bin for previous values.
  • bottom - It denotes location of baseline of each bin.
  • histtype - It denotes the type of histogram to be plotted - bar,bar stacked,step or step filled.
  • align - Alignment position of the histogram - left, right or middle(default).
  • orientation - It denotes whether you want to plot your histogram horizontally or vertically(default).
  • rwidth - It denotes the relative width of the bars with respect to bin width.

Plot a simple histogram in Matplotlib

Let's build a histogram of basic distribution. You can use the random function from NumPy for the distribution.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
 
# Using numpy random function to generate random data
np.random.seed(19685689)
 
mu, sigma = 100, 20
x = mu + sigma * np.random.randn(10000)
 
# passing the histogram function
n, bins, patches = plt.hist(x, 50, histtype='bar', density=True)
 
 
plt.xlabel('Values')
plt.ylabel('Probability Distribution')
plt.title('Simple histogram with Matplotlib')
plt.xlim(40, 180)
plt.ylim(0, 0.04)
plt.grid(True)
plt.show()
Simple histogram with Matplotlib

Plotting multiple datasets with different color in a histogram

If you are trying to plot multiple datasets in a histogram, you can differentiate each dataset with a different color.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
 
# Using numpy random function to generate random data
np.random.seed(19685689)
 
x = np.random.randn(500000, 3)
colors = ['blue', 'black', 'red']

# passing the histogram function
n, bins, patches = plt.hist(x, 50, histtype='bar', density=True, color=colors)

plt.title('Simple histogram with Matplotlib')
plt.grid(True)
plt.show()

Pylenin has a dedicated Youtube playlist for Matplotlib Tutorial. Check out our entire Matplotlib playlist here.

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe