生成带有颜色的热图作为两个截距之间的实例数量
Generate heatmap with color as amount of instances between two intercepts
我有很多数据,肉眼几乎无法将其解释为 xy 散点图。对于 mit 它更有趣的是在哪里构建集群,这就是为什么我选择了热图的想法:
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 bin 应该有大约 14 个元素,如颜色条所示。
我决定自己打字,这里是为所有寻找基本解决方案的人准备的 (thanks to)。根据需要,块中心的 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 "_______"
这是结果。
和
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 |
注意:我不能保证印刷结果是否正确。使用线印验证其正确性
我有很多数据,肉眼几乎无法将其解释为 xy 散点图。对于 mit 它更有趣的是在哪里构建集群,这就是为什么我选择了热图的想法:
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 bin 应该有大约 14 个元素,如颜色条所示。
我决定自己打字,这里是为所有寻找基本解决方案的人准备的 (thanks to)。根据需要,块中心的 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 "_______"
这是结果。
和
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 |
注意:我不能保证印刷结果是否正确。使用线印验证其正确性