Skip to content

Instantly share code, notes, and snippets.

@emptymalei
Forked from teechap/heatmap_example.py
Created December 10, 2015 05:11
Show Gist options
  • Save emptymalei/5a445e16e7f216089f6e to your computer and use it in GitHub Desktop.
Save emptymalei/5a445e16e7f216089f6e to your computer and use it in GitHub Desktop.
How to make a heatmap from data stored in Python lists
'''
Most heatmap tutorials I found online use pyplot.pcolormesh with random sets of
data from Numpy; I just needed to plot x, y, z values stored in lists--without
all the Numpy mumbo jumbo. Here I have code to plot intensity on a 2D array, and
I only use Numpy where I need to (pcolormesh expects Numpy arrays as inputs).
'''
import matplotlib.pyplot as plt
import numpy as np
#here's our data to plot, all normal Python lists
x = [1, 2, 3, 4, 5]
y = [0.1, 0.2, 0.3, 0.4, 0.5]
intensity = [
[5, 10, 15, 20, 25],
[30, 35, 40, 45, 50],
[55, 60, 65, 70, 75],
[80, 85, 90, 95, 100],
[105, 110, 115, 120, 125]
]
#setup the 2D grid with Numpy
x, y = np.meshgrid(x, y)
#convert intensity (list of lists) to a numpy array for plotting
intensity = np.array(intensity)
#now just plug the data into pcolormesh, it's that easy!
plt.pcolormesh(x, y, intensity)
plt.colorbar() #need a colorbar to show the intensity scale
plt.show() #boom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment