【发布时间】:2018-07-06 10:26:06
【问题描述】:
如何从 Python 中的 2D 散点图数据创建热图,散点图中的每个 (x,y) 点都有一个与之关联的 z 值? z 值将是用于为热图着色的值。
例如,在 R 中,我可以使用:
# This example is from http://knowledge-forlife.com/r-creating-heatmap-scatterplot-data/
#I'm just setting the seed so you can see the same example on your computer
set.seed(1)
#Our X data
x <- runif(150)
#Our Y data
y <- runif(150)
#Our Z data
z <- c(rnorm(mean=1,100),rnorm(mean=20,50))
#Store the length of our data
N <- length(x)
# View the scatterplot
plot(x, y)
#Here is the interpolation to give the heatmap effect.
#Use xo and yo to set the output grid you want to use.
#xo and yo are used to change the resolution of the interpolation
#Here, I have included a somewhat standard protocol for these parameters
s <- interp(x,y,z,xo=seq(min(x),max(x),length=N),
yo=seq(min(x),max(x),length=N),duplicate="mean")
#Here's where the fun happens
#Note you can add your typical plotting paramaters here, such as xlab or ylab
image.plot(s,xlim=c(0,1),ylim=c(0,1),zlim=c(-2,25))
散点图(此散点图中的每个 (x,y) 点都有一个与之关联的 z 值;z 值在散点图中不可见):
对应的热图(颜色代表z值):
请注意,此问题与 Generate a heatmap in MatPlotLib using a scatter data set 不同,其中热图中的颜色表示 (x,y) 点的密度。
【问题讨论】: