Skip to content

Instantly share code, notes, and snippets.

@teechap
Created September 18, 2014 00:35
Show Gist options
  • Select an option

  • Save teechap/9c066a9ab054cc322877 to your computer and use it in GitHub Desktop.

Select an option

Save teechap/9c066a9ab054cc322877 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
@mkamyab
Copy link
Copy Markdown

mkamyab commented Jul 10, 2017

Is there a way to handle missing data None?

@supersaiyan12
Copy link
Copy Markdown

Thanks for the small code !!, it's really helpful

@lorenzoayub
Copy link
Copy Markdown

Thank you very much! It really helped me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment