使用Matplotlib中的3D数据生成热图 [英] Generating a heat map using 3D data in matplotlib

查看:363
本文介绍了使用Matplotlib中的3D数据生成热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数returnValuesAtTime,该函数返回三个列表-x_valsy_valsswe_vals.所有三个列表的长度相同,并且swe_vals中的每个元素对应于x_vals中的x-valuey_vals中的y-value.我希望生成一个带有图例的热图,该图例使用(x,y)坐标和swe_vals作为强度.

I have a function returnValuesAtTime that returns three lists-x_vals,y_vals and swe_vals. All three lists are of the same length and each element in swe_vals corresponds to a x-value from x_vals and a y-value from y_vals. I wish to generate a heatmap with a legend that uses the (x,y) coordinates and the swe_vals as the intensity.

我编写了以下代码:

def plotValuesAtTimeMap(t):
    x_vals,y_vals,swe_vals=returnValuesAtTime(t)
    x_points=len(x_vals)
    y_points=len(y_vals)
    xx=np.linspace(x_vals[0],x_vals[-1],x_points)
    yy=np.linspace(y_vals[0],y_vals[-1],y_points)
    fig,ax=plt.subplots()
    im=ax.pcolormesh(xx,yy,z)
    fig.colorbar(im)
    ax.axis('tight')
    plt.show()

一旦使用returnValuesAtTime(t)获得了三个列表,我将采用x_valsy_vals的长​​度.然后,我用它们生成x和y方向的间距,其极限是x_valsy_vals的第一个和最后一个元素.然后,我尝试生成colormesh.但这给了我一个没有值的空colormesh.

Once the three lists are obtained using returnValuesAtTime(t), I take the lengths of x_vals and y_vals. I then use these to generate the spacing for x and y-directions, with the limits being the first and last elements of x_vals and y_vals. I then try to generate the colormesh. But this gives me an empty colormesh with no values.

可能出什么问题了?使用3D numpy数组而不是三个列表是否可以解决问题?

What might be going wrong? Will using a 3D numpy array instead of the three lists solve the problem?

每个列表的前10个元素是:

The first 10 elements of each of the lists are:

x_vals[0:10]

[439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893,
 439936.86573189893]

y_vals[0:10]

[4514018.2797159087,
 4513898.2797159087,
 4513778.2797159087,
 4513658.2797159087,
 4513538.2797159087,
 4513418.2797159087,
 4513298.2797159087,
 4513178.2797159087,
 4513058.2797159087,
 4512938.2797159087]

swe_vals[0:10]

[2.7520323,
 2.7456229,
 2.7456021,
 2.745455,
 2.7478349,
 2.7445269,
 2.7484877,
 2.7524617,
 2.75491,
 2.7509627]

修改:

我添加了一个散点图,显示下面的(x,y)网格范围:

I have added a scatter plot showing the (x,y) grid range below:

x和y值在列表中,每个长度为6804.每个(x,y)点在单独的列表中都有一个对应的z值,其长度也为6804.为了阐明我希望实现的目标,我想生成一个像热图一样的热图,其z轴的大小由该图上每个网格的颜色表示.如下所示:

The x and y values are in lists, each 6804 in length. Each (x,y) point has a corresponding z-value in a separate list, which is also 6804 in length. To clarify what I hope to achieve, I want to generate a heatmap like plot with the magnitude of z-axis represented by the color of each grid on the plot. Something like what's shown below:

在示例图中,所有z值都相同.因此,整个网格空间的颜色都是相同的.

In the example plot, all z-values are the same. So the color is same across the entire grid space.

使用新的合成图进行编辑(根据朱CT成员的建议):

推荐答案

如果将xyz整形为方矩阵,则可以执行contourf绘图:

It looks like if reshape x, y, z to square matrix, you can do a contourf plot:

In [7]:X
Out[7]:
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

In [8]:Y
Out[8]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
       [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
       [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])

plt.contourf(X,Y,np.random.random((10,10))) #reshape Z too!
plt.colorbar()

这篇关于使用Matplotlib中的3D数据生成热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆