Skip to content
Home » matplotlib Part 19 – Histograms

matplotlib Part 19 – Histograms

Spread the love

Histograms

In this article we’ll have a look at histograms, binnings and density plots. Let’s create a simple histogram using random data:

In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

# Let's create some random data.
data = np.random.randn(1000)

# We use the hist method to create a histogram.
fig, ax = plt.subplots()
ax.hist(data)
Out[1]:
(array([  9.,  30., 106., 188., 273., 215., 117.,  48.,  11.,   3.]),
 array([-3.09166593, -2.42446317, -1.7572604 , -1.09005764, -0.42285488,
         0.24434788,  0.91155064,  1.57875341,  2.24595617,  2.91315893,
         3.58036169]),
 <a list of 10 Patch objects>)

There are lots of arguments you can set to fine-tune your chart. You can change the number of bins, which is by default set to 10, as you can see above. The bins are of equal width. Let’s set the bin argument to some other values:

In [2]:
# 3 bins
fig, ax = plt.subplots()
ax.hist(data, bins=3)
Out[2]:
(array([195., 713.,  92.]),
 array([-3.09166593, -0.86765672,  1.35635249,  3.58036169]),
 <a list of 3 Patch objects>)
In [3]:
# 30 bins
fig, ax = plt.subplots()
ax.hist(data, bins=30)
Out[3]:
(array([ 2.,  2.,  5.,  7.,  9., 14., 29., 34., 43., 50., 63., 75., 93.,
        92., 88., 77., 80., 58., 53., 34., 30., 18., 18., 12.,  7.,  2.,
         2.,  2.,  0.,  1.]),
 array([-3.09166593, -2.86926501, -2.64686409, -2.42446317, -2.20206225,
        -1.97966132, -1.7572604 , -1.53485948, -1.31245856, -1.09005764,
        -0.86765672, -0.6452558 , -0.42285488, -0.20045396,  0.02194696,
         0.24434788,  0.4667488 ,  0.68914972,  0.91155064,  1.13395156,
         1.35635249,  1.57875341,  1.80115433,  2.02355525,  2.24595617,
         2.46835709,  2.69075801,  2.91315893,  3.13555985,  3.35796077,
         3.58036169]),
 <a list of 30 Patch objects>)

You can change the color of the bins:

In [4]:
# green color
fig, ax = plt.subplots()
ax.hist(data, color='g')
Out[4]:
(array([  9.,  30., 106., 188., 273., 215., 117.,  48.,  11.,   3.]),
 array([-3.09166593, -2.42446317, -1.7572604 , -1.09005764, -0.42285488,
         0.24434788,  0.91155064,  1.57875341,  2.24595617,  2.91315893,
         3.58036169]),
 <a list of 10 Patch objects>)

You can change the type of the histogram. To do that, you just have to set the histtype argument to one of the predefined values. By default it’s set to ‘bar’. Here it’s set to ‘step’:

In [5]:
# step histtype
fig, ax = plt.subplots()
ax.hist(data, histtype='step')
Out[5]:
(array([  9.,  30., 106., 188., 273., 215., 117.,  48.,  11.,   3.]),
 array([-3.09166593, -2.42446317, -1.7572604 , -1.09005764, -0.42285488,
         0.24434788,  0.91155064,  1.57875341,  2.24595617,  2.91315893,
         3.58036169]),
 <a list of 1 Patch objects>)

By default the histogram is vertical, but you can change its orientation to horizontal:

In [6]:
# horizontal orientation
fig, ax = plt.subplots()
ax.hist(data, orientation='horizontal')
Out[6]:
(array([  9.,  30., 106., 188., 273., 215., 117.,  48.,  11.,   3.]),
 array([-3.09166593, -2.42446317, -1.7572604 , -1.09005764, -0.42285488,
         0.24434788,  0.91155064,  1.57875341,  2.24595617,  2.91315893,
         3.58036169]),
 <a list of 10 Patch objects>)

Here’s how you can set the edge color:

In [7]:
# red edge
fig, ax = plt.subplots()
ax.hist(data, edgecolor='r')
Out[7]:
(array([  9.,  30., 106., 188., 273., 215., 117.,  48.,  11.,   3.]),
 array([-3.09166593, -2.42446317, -1.7572604 , -1.09005764, -0.42285488,
         0.24434788,  0.91155064,  1.57875341,  2.24595617,  2.91315893,
         3.58036169]),
 <a list of 10 Patch objects>)

If you need logarithmic scale, you can set the log argument to True:

In [8]:
# logarithmic scale
fig, ax = plt.subplots()
ax.hist(data, log=True)
Out[8]:
(array([  9.,  30., 106., 188., 273., 215., 117.,  48.,  11.,   3.]),
 array([-3.09166593, -2.42446317, -1.7572604 , -1.09005764, -0.42285488,
         0.24434788,  0.91155064,  1.57875341,  2.24595617,  2.91315893,
         3.58036169]),
 <a list of 10 Patch objects>)

You can also set the alpha value:

In [9]:
# alpha
fig, ax = plt.subplots()
ax.hist(data, alpha=.3)
Out[9]:
(array([  9.,  30., 106., 188., 273., 215., 117.,  48.,  11.,   3.]),
 array([-3.09166593, -2.42446317, -1.7572604 , -1.09005764, -0.42285488,
         0.24434788,  0.91155064,  1.57875341,  2.24595617,  2.91315893,
         3.58036169]),
 <a list of 10 Patch objects>)

As usual, check out the documentation for more options.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply