matplotlib的热图 [英] Heatmap with matplotlib
问题描述
我有一组三元组,每个三元组由(var1,var2,result)组成。
包含9个三元组的列表的示例:
I have a set of 3-tuples, each 3-tuple consists of (var1, var2, result). Example of a list of 9 3-tuples:
<type 'list'>:
[(4, 0.7, 0.8530612244898, 0.016579670213527985),
(4, 0.6, 0.8730158730157779, 0.011562402525241757),
(6, 0.8, 0.8378684807257778, 0.018175985875060037),
(4, 0.8, 0.8605442176870833, 0.015586992159716321),
(2, 0.8, 0.8537414965986667, 0.0034013605443334316),
(2, 0.7, 0.843537414966, 0.006802721088333352),
(6, 0.6, 0.8480725623582223, 0.01696896774039503),
(2, 0.6, 0.84693877551025, 0.010204081632749995),
(6, 0.7, 0.8577097505669444, 0.019873637350220318)]
现在,我想以此创建一个热图。
Var1
可以是值 [2,4,6]
和 Var2
可以为 [0.6、0.7、0.8]
。共有9个结果。
Now I'd like to create a heatmap out of this.
Var1
can be values [2,4,6]
and Var2
can be [0.6, 0.7, 0.8]
. There's a total of 9 results.
这是我用来绘制热图的代码:
This is the code I use to plot a heatmap:
# list of 3-tuples to 3 lists: x, y and weights
# x (var1) = [2,4,6]
# y (var2) = [0.6, 0.7, 0.8]
# weights (res) = [....] (9 values)
x, y = np.meshgrid(x, y)
intensity = np.array(weights)
plt.pcolormesh(x, y, intensity)
plt.colorbar() # need a colorbar to show the intensity scale
plt.show()
这会导致此图:
Which leads to this graph:
我觉得很奇怪,那里只有4个部分,但我希望有9个(3x3)。有人可以说明我在这里做错了什么吗?
What I find weird is that there's only 4 sections, but I expected there to be 9 (3x3). Can someone shed some light on what I did wrong here?
推荐答案
meshgrid
正在创建九个 points ,而不是九个补丁。
查看输出。您在(2,0.6)上获得了1分,在(2,0.7)上获得了1分,等等。
meshgrid
is creating nine points, not nine patches.
Look at the output. You've got one point at (2, 0.6), one at (2, 0.7), etc.
这篇关于matplotlib的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!