Skip to content
Home » matplotlib Part 20 – 2D Histograms

matplotlib Part 20 – 2D Histograms

Spread the love

2D Histograms

In the previous article we were talking about histograms. Today we’ll be talking about two-dimensional histograms, which we create using the hist2d method. Let’s define some input values and then create a 2D histogram:

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

# random data for x and y
x = np.random.randn(1000)
y = np.random.randn(1000)

# Let's plot the 2D histogram.
fig, ax = plt.subplots()
ax.hist2d(x, y)
Out[1]:
(array([[ 0.,  0.,  0.,  0.,  1.,  1.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  1.,  0.,  3.,  2.,  3.,  0.,  0.,  0.],
        [ 0.,  0.,  1.,  1., 11., 13.,  7.,  5.,  6.,  1.],
        [ 0.,  0.,  3., 11., 19., 33., 43., 28.,  7.,  2.],
        [ 0.,  0.,  7., 21., 31., 59., 69., 35., 13.,  1.],
        [ 0.,  0.,  7., 23., 41., 68., 60., 41., 15.,  6.],
        [ 2.,  1.,  3., 12., 32., 39., 42., 42., 13.,  2.],
        [ 0.,  0.,  0.,  4., 14., 22., 24.,  7.,  1.,  1.],
        [ 0.,  0.,  0.,  1.,  7.,  9.,  7.,  3.,  1.,  1.],
        [ 0.,  0.,  0.,  1.,  0.,  2.,  3.,  3.,  1.,  1.]]),
 array([-3.55448897, -2.8764872 , -2.19848542, -1.52048365, -0.84248187,
        -0.1644801 ,  0.51352168,  1.19152346,  1.86952523,  2.54752701,
         3.22552878]),
 array([-4.00630263, -3.33061748, -2.65493233, -1.97924719, -1.30356204,
        -0.62787689,  0.04780826,  0.7234934 ,  1.39917855,  2.0748637 ,
         2.75054885]),
 <matplotlib.collections.QuadMesh at 0x1d9165638c8>)

Just like one-dimensional histograms, the default number of bins is 10. It’s 10 for each dimension. Let’s change it to be 40 bins for each dimension:

In [2]:
fig, ax = plt.subplots()
ax.hist2d(x, y, bins=40)
Out[2]:
(array([[0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        ...,
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.],
        [0., 0., 0., ..., 0., 0., 0.]]),
 array([-3.55448897, -3.38498853, -3.21548809, -3.04598764, -2.8764872 ,
        -2.70698675, -2.53748631, -2.36798587, -2.19848542, -2.02898498,
        -1.85948453, -1.68998409, -1.52048365, -1.3509832 , -1.18148276,
        -1.01198232, -0.84248187, -0.67298143, -0.50348098, -0.33398054,
        -0.1644801 ,  0.00502035,  0.17452079,  0.34402124,  0.51352168,
         0.68302212,  0.85252257,  1.02202301,  1.19152346,  1.3610239 ,
         1.53052434,  1.70002479,  1.86952523,  2.03902567,  2.20852612,
         2.37802656,  2.54752701,  2.71702745,  2.88652789,  3.05602834,
         3.22552878]),
 array([-4.00630263, -3.83738134, -3.66846006, -3.49953877, -3.33061748,
        -3.16169619, -2.99277491, -2.82385362, -2.65493233, -2.48601105,
        -2.31708976, -2.14816847, -1.97924719, -1.8103259 , -1.64140461,
        -1.47248333, -1.30356204, -1.13464075, -0.96571946, -0.79679818,
        -0.62787689, -0.4589556 , -0.29003432, -0.12111303,  0.04780826,
         0.21672954,  0.38565083,  0.55457212,  0.7234934 ,  0.89241469,
         1.06133598,  1.23025727,  1.39917855,  1.56809984,  1.73702113,
         1.90594241,  2.0748637 ,  2.24378499,  2.41270627,  2.58162756,
         2.75054885]),
 <matplotlib.collections.QuadMesh at 0x1d913d7c348>)

You can also specify the number of bins individually for each dimension. Let’s make it 30 and 10 respectively:

In [3]:
fig, ax = plt.subplots()
ax.hist2d(x, y, bins=[30, 10])
Out[3]:
(array([[ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
        [ 0.,  0.,  1.,  0.,  1.,  2.,  3.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  1.,  1.,  3.,  2.,  1.,  1.],
        [ 0.,  0.,  1.,  1.,  4.,  5.,  3.,  1.,  1.,  0.],
        [ 0.,  0.,  0.,  0.,  6.,  7.,  1.,  2.,  4.,  0.],
        [ 0.,  0.,  2.,  1.,  2.,  7., 10.,  7.,  1.,  0.],
        [ 0.,  0.,  0.,  3.,  5., 11., 11.,  7.,  3.,  0.],
        [ 0.,  0.,  1.,  7., 12., 15., 22., 14.,  3.,  2.],
        [ 0.,  0.,  2.,  7.,  7., 13., 22., 10.,  7.,  1.],
        [ 0.,  0.,  1.,  8., 11., 20., 25., 11.,  4.,  0.],
        [ 0.,  0.,  4.,  6., 13., 26., 22., 14.,  2.,  0.],
        [ 0.,  0.,  2.,  6., 16., 28., 18., 14.,  5.,  1.],
        [ 0.,  0.,  2., 10., 12., 22., 23., 17.,  6.,  3.],
        [ 0.,  0.,  3.,  7., 13., 18., 19., 10.,  4.,  2.],
        [ 1.,  0.,  1.,  7., 16., 14., 18., 16.,  4.,  1.],
        [ 1.,  1.,  1.,  2.,  7., 12., 19., 10.,  2.,  1.],
        [ 0.,  0.,  1.,  3.,  9., 13.,  5., 16.,  7.,  0.],
        [ 0.,  0.,  0.,  1.,  7.,  8., 11.,  4.,  1.,  0.],
        [ 0.,  0.,  0.,  3.,  3.,  5.,  8.,  2.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  4.,  9.,  5.,  1.,  0.,  1.],
        [ 0.,  0.,  0.,  1.,  6.,  4.,  3.,  2.,  1.,  1.],
        [ 0.,  0.,  0.,  0.,  0.,  2.,  2.,  1.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  1.,  3.,  2.,  0.,  0.,  0.],
        [ 0.,  0.,  0.,  1.,  0.,  0.,  1.,  1.,  0.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.,  0.],
        [ 0.,  0.,  0.,  0.,  0.,  1.,  1.,  1.,  0.,  1.]]),
 array([-3.55448897, -3.32848838, -3.10248779, -2.8764872 , -2.65048661,
        -2.42448601, -2.19848542, -1.97248483, -1.74648424, -1.52048365,
        -1.29448305, -1.06848246, -0.84248187, -0.61648128, -0.39048069,
        -0.1644801 ,  0.0615205 ,  0.28752109,  0.51352168,  0.73952227,
         0.96552286,  1.19152346,  1.41752405,  1.64352464,  1.86952523,
         2.09552582,  2.32152641,  2.54752701,  2.7735276 ,  2.99952819,
         3.22552878]),
 array([-4.00630263, -3.33061748, -2.65493233, -1.97924719, -1.30356204,
        -0.62787689,  0.04780826,  0.7234934 ,  1.39917855,  2.0748637 ,
         2.75054885]),
 <matplotlib.collections.QuadMesh at 0x1d91715de48>)

There are some more options you can tweak, but we’re not going to cover them in this series.

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