gpt4 book ai didi

python - 在python中绘制热图

转载 作者:太空宇宙 更新时间:2023-11-03 14:07:45 24 4
gpt4 key购买 nike

我有两个列表 x, y 表示二维坐标。例如 x = [1,4,0​​.5,2,5,10,33,0.04]y = [2,5,44,0.33,2,14,20,0.03 ]x[i]y[i] 表示二维中的一个点。现在我还有一个表示每个 (x,y) 点的“热”值的列表,例如 z = [0.77, 0.88, 0.65, 0.55, 0.89, 0.9, 0.8,0.95]。当然,x、y 和 z 的维度比示例高得多。

现在我想绘制一个二维热图,其中 x 和 y 代表轴坐标,z 代表颜色。这在 Python 中如何完成?

最佳答案

此代码生成热图。有了更多的数据点,绘图开始看起来非常漂亮,而且我发现它通常非常快,即使对于 >100k 的点也是如此。

import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np
import math

x = [1,4,0.5,2,5,10,33,0.04]
y = [2,5,44,0.33,2,14,20,0.03]
z = [0.77, 0.88, 0.65, 0.55, 0.89, 0.9, 0.8, 0.95]
levels = [0.7, 0.75, 0.8, 0.85, 0.9]

plt.figure()
ax = plt.gca()
ax.set_aspect('equal')
CS = ax.tricontourf(x, y, z, levels, cmap=plt.get_cmap('jet'))
cbar = plt.colorbar(CS, ticks=np.sort(np.array(levels)),ax=ax, orientation='horizontal', shrink=.75, pad=.09, aspect=40,fraction=0.05)
cbar.ax.set_xticklabels(list(map(str,np.sort(np.array(levels))))) # horizontal colorbar
cbar.ax.tick_params(labelsize=8)
plt.title('Heat Map')
plt.xlabel('X Label')
plt.ylabel('Y Label')

plt.show()

生成这张图片:

enter image description here

或者如果您正在寻找更渐进的颜色变化,请将 tricontourf 线更改为:

CS = ax.tricontourf(x, y, z, np.linspace(min(levels),max(levels),256), cmap=cmap)

然后剧情会变成:

enter image description here

关于python - 在python中绘制热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41962146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com