python logo

Generate heatmap in Matplotlib

Python hosting: Host, run, and code Python in the cloud!

A heatmap can be created using Matplotlib and numpy.

Related courses
If you want to learn more on data visualization, this course is 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 64x64. 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.clf()
plt.title('Pythonspot.com heatmap example')
plt.ylabel('y')
plt.xlabel('x')
plt.imshow(heatmap, extent=extent)
plt.show()

Result:

matplot-heatmap Matplotlib heatmap

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

 

BackNext





Leave a Reply: