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]:
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]:
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]:
There are some more options you can tweak, but we’re not going to cover them in this series.