简体   繁体   English

matplotlib 3D热图

[英]matplotlib 3D heatmap

How to use pcolormesh to plot a heatmap? 如何使用pcolormesh绘制热图? I have three lists of equal size, X, Y and Z. When I do 我有三个大小相等的列表,分别是X,Y和Z。

plt.pcolormesh(X, Y, Z)

I get "ValueError: need more than 1 value to unpack" and when I do 我收到“ ValueError:需要多个值来解压缩”,当我这样做时

plt.pcolormesh(np.array(zip(X, Y)), Z)

I get this: 我得到这个: 在此处输入图片说明

You have to look at the documentation for pcolor to see the requirements for the input arguments to pcolormesh. 您必须查看pcolor的文档才能查看pcolormesh输入参数的要求。 x, y and c can't be lists of numbers, they have be lists of lists or two dimensional numpy arrays. x,y和c不能是数字列表,它们可以是列表列表或二维numpy数组。 You need two dimensional arrays because pcolor and pcolormesh draw a quadrilateral for each value of c with corners defined in x and y. 您需要二维数组,因为pcolor和pcolormesh会为每个c值绘制一个四边形,并且在x和y中定义了边角。 The x and y values that correspond to a particular value in c are determined by their location in the array. 对应于c中特定值的x和y值由它们在数组中的位置确定。

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和Y,如果给定,则指定彩色四边形的(x,y)坐标; C [i,j]的四边形在以下位置有角:

(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])." (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 . 要将x和y列表更改为二维numpy数组,可以使用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()

pcolormesh示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM