三个变量作为热图 [英] Three variables as heatmap

查看:102
本文介绍了三个变量作为热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据绘制为具有以下结构的热图:

I want to plot my data as a heatmap which has the following structure:

X = [1,1,1,1,1,1,1,1,1,1], Y = [1,2,3,4,5,6,7,8,9,10] Z = [0.2, 0.33, 0.1, 0.25, 0.0, 0.9, 0.75, 0.88, 0.44, 0.95]

X = [1,1,1,1,1,1,1,1,1,1], Y = [1,2,3,4,5,6,7,8,9,10] Z = [0.2, 0.33, 0.1, 0.25, 0.0, 0.9, 0.75, 0.88, 0.44, 0.95]

x和y轴分别由X和Y表示,而热"由Z值表示.

The x and y-axis shall be represented by X and Y, while the 'heat' is represented by the values of Z.

例如在坐标(x,y)=(1,2)时强度应为0.33 如何通过使用matplotlib来实现? 在查看与关键字热图甚至与等高线图有关的帖子时,我还无法将其转移到这个问题上.

E.g. at coordinate (x,y) = (1,2) the intensity shall be 0.33 How can this be achieved by using matplotlib? Looking at posts which relate to the keyword heatmap or even to those related to the term contour map, I could not transfer it to this problem yet.

在此先感谢您的提示 丹

Thank you in advance for any hints Dan

推荐答案

我希望您的数据只是一个例子,因为它看起来很有趣(它更多是带状序列; x维度是恒定的).

I hope your data is just an example because it will look funny (it's more a sequence of strips; the x-dimension is constant).

我建议使用 pandas(常规数据分析)

I would recommend the usage of pandas (general data-analysis) and seaborn (matplotlib-extensions) which makes it a bit nicer.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
X = [1,1,1,1,1,1,1,1,1,1]
Y = [1,2,3,4,5,6,7,8,9,10]
Z = [0.2, 0.33, 0.1, 0.25, 0.0, 0.9, 0.75, 0.88, 0.44, 0.95]
data = pd.DataFrame({'X': X, 'Y': Y, 'Z': Z})
data_pivoted = data.pivot("X", "Y", "Z")
ax = sns.heatmap(data_pivoted)
plt.show()

输出

这篇关于三个变量作为热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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