Generate heatmap in Matplotlib

A heatmap can be created using Matplotlib and numpy.

Related courses
If you want to learn more on data visualization, these courses are good:

Heatmap example

The histogram2d function can be used to generate a heatmap.

We create some random data arrays (x,y) to use in the program. We set bins to 64, the resulting heatmap will be 64×64. If you want another size change the number of bins.

import numpy as np
import numpy.random
import matplotlib.pyplot as plt
# Create data
x = np.random.randn(4096)
y = np.random.randn(4096)
# Create heatmap
heatmap, xedges, yedges = np.histogram2d(x, y, bins=(64,64))
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
# Plot heatmap
plt.title(' heatmap example')
plt.imshow(heatmap, extent=extent)


Matplotlib heatmap

The datapoints in this example are totally random and generated using np.random.randn()