Python 以颜色作为两次截取之间实例的数量生成热图
我有很多数据几乎不能用肉眼解释为xy散点图。对于麻省理工学院来说,更有趣的是集群是在哪里建立的这就是为什么我选择了热图的想法:Python 以颜色作为两次截取之间实例的数量生成热图,python,matplotlib,plot,heatmap,data-science,Python,Matplotlib,Plot,Heatmap,Data Science,我有很多数据几乎不能用肉眼解释为xy散点图。对于麻省理工学院来说,更有趣的是集群是在哪里建立的这就是为什么我选择了热图的想法: heatmap, yedges, xedges = np.histogram2d(y, x, bins=(10,10)) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] 生成以下绘图 这很好。然而,我甚至不确定这种颜色表示什么,但它不是特定范围(例如4>x>5&11>y>12)之间的数据点数量 问题 我
heatmap, yedges, xedges = np.histogram2d(y, x, bins=(10,10))
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
生成以下绘图
这很好。然而,我甚至不确定这种颜色表示什么,但它不是特定范围(例如4>x>5&11>y>12
)之间的数据点数量
问题
我知道我可以编写一个程序来合并适当的数据点,计算一个单元的实例并自己绘制所需的热图,但在数据科学中还没有这样一个简洁工具的实现吗?你可以使用matplotlib hexbin实现一个简单的方法,或者在seaborn中查看kde图。我不确定我是否理解你关于计数的评论。你认为它们放错地方了吗?由于矩阵方向与其他语言不同,通常会混淆轴的原点或需要转置矩阵。除此之外,~(8,12)处的2D容器应该有14个元素,如颜色栏所示。我决定自己键入它,这是给所有寻找基本解决方案的人的()。根据需要,X值位于块的中心:
import numpy as np
import matplotlib.pyplot as plt
def makeOwnHeatMap(x,y,bins):
#shift +/- for the axes labels and
xMin = float(int(min(x)))-0.5
xMax = float(int(max(x)))+0.5
yMin = float(int(min(y)))-0.5
yMax = float(int(max(y)))+0.5
yStep = float(yMax-yMin)/bins[0]
xStep = float(xMax-xMin)/bins[1]
downscaledGraph = np.zeros((bins[0],bins[1]))
#make heatmap
for i in range(0,len(y)):
curY = y[i] #current y-value from data
curX = x[i] #current x-value from data
yetY = 0 #current y compare value within a stepsize
yetX = 0 #current x compare value within a stepsize
cntY = 0 #counter y for matrix coordinates
cntX = 0 #counter x for matrix coodrinates
while (yetY < curY-yMin):
yetY += yStep
cntY += 1
while (yetX < curX-xMin):
yetX += xStep
cntX += 1
#ends up with incrementing 1 x too much
cntY -= 1
cntX -= 1
downscaledGraph[cntY,cntX] += 1
#make axes labels
xbar = []
ybar = []
thisY = yMin
while thisY <= yMax:
ybar.append(thisY)
thisY += yStep
thisX = xMin
while thisX <= xMax:
xbar.append(thisX)
thisX += xStep
#draw heatmap
xbar, ybar = np.meshgrid(xbar, ybar)
intensity = np.array(downscaledGraph)
plt.pcolormesh(xbar, ybar, intensity)
plt.show()
for i in range(0,bins[0]):
for j in range(0, bins[1]):
print downscaledGraph[i,j],"\t",
print "|"
print "_______"
注意:我不能保证它的结果是否正确。使用lineprint验证其正确性
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 |
0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 |
1.0 0.0 12.0 0.0 0.0 0.0 0.0 0.0 |
18.0 0.0 7.0 0.0 0.0 16.0 0.0 0.0 |
8.0 0.0 7.0 0.0 0.0 10.0 0.0 1.0 |
15.0 0.0 6.0 0.0 0.0 12.0 0.0 7.0 |
0.0 0.0 3.0 0.0 0.0 3.0 0.0 6.0 |
0.0 0.0 4.0 0.0 0.0 1.0 0.0 0.0 |
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 |
0.0 0.0 2.0 0.0 0.0 0.0 0.0 0.0 |