Reputation: 2317
How to use pcolormesh to plot a heatmap? I have three lists of equal size, X, Y and Z. When I do
plt.pcolormesh(X, Y, Z)
I get "ValueError: need more than 1 value to unpack" and when I do
plt.pcolormesh(np.array(zip(X, Y)), Z)
I get this:
Upvotes: 0
Views: 4655
Reputation: 13610
You have to look at the documentation for pcolor to see the requirements for the input arguments to pcolormesh. x, y and c can't be lists of numbers, they have be lists of lists or two dimensional numpy arrays. You need two dimensional arrays because pcolor and pcolormesh draw a quadrilateral for each value of c with corners defined in x and y. The x and y values that correspond to a particular value in c are determined by their location in the array.
From the documentation:
"X and Y, if given, specify the (x, y) coordinates of the colored quadrilaterals; the quadrilateral for C[i,j] has corners at:
(X[i, j], Y[i, j]), (X[i, j+1], Y[i, j+1]), (X[i+1, j], Y[i+1, j]), (X[i+1, j+1], Y[i+1, j+1])."
To change your x and y lists into two dimensional numpy arrays you can use meshgrid.
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,11)
y = np.arange(0,11)
xv, yv = np.meshgrid(x,y)
c = np.random.rand(10,10)
plt.pcolormesh(xv,yv,c)
plt.show()
Upvotes: 1