我有一组X,Y数据点(大约10k),很容易绘制成散点图,但我想用热图来表示。
我查看了Matplotlib中的示例,它们似乎都已经从热图单元值开始生成图像。
有没有一种方法可以将一堆不同的x, y转换为热图(其中x, y频率较高的区域会“更温暖”)?
我有一组X,Y数据点(大约10k),很容易绘制成散点图,但我想用热图来表示。
我查看了Matplotlib中的示例,它们似乎都已经从热图单元值开始生成图像。
有没有一种方法可以将一堆不同的x, y转换为热图(其中x, y频率较高的区域会“更温暖”)?
当前回答
下面是我在100万个点集上做的一个,有3个类别(红色、绿色和蓝色)。如果您想尝试这个功能,这里有一个到存储库的链接。Github回购
histplot(
X,
Y,
labels,
bins=2000,
range=((-3,3),(-3,3)),
normalize_each_label=True,
colors = [
[1,0,0],
[0,1,0],
[0,0,1]],
gain=50)
其他回答
下面是我在100万个点集上做的一个,有3个类别(红色、绿色和蓝色)。如果您想尝试这个功能,这里有一个到存储库的链接。Github回购
histplot(
X,
Y,
labels,
bins=2000,
range=((-3,3),(-3,3)),
normalize_each_label=True,
colors = [
[1,0,0],
[0,1,0],
[0,0,1]],
gain=50)
最初的问题是…如何将散点值转换为网格值? Histogram2d确实计算每个单元格的频率,但是,如果每个单元格除了频率之外还有其他数据,则需要做一些额外的工作。
x = data_x # between -10 and 4, log-gamma of an svc
y = data_y # between -4 and 11, log-C of an svc
z = data_z #between 0 and 0.78, f1-values from a difficult dataset
我有一个数据集,X和Y坐标的z结果。然而,我计算的是兴趣区域之外的几个点(大的差距),而在一个小的兴趣区域内的一堆点。
是的,在这里它变得更困难,但也更有趣。一些库(抱歉):
from matplotlib import pyplot as plt
from matplotlib import cm
import numpy as np
from scipy.interpolate import griddata
Pyplot是我今天的图形引擎, Cm是一个彩色地图的范围,有一些有趣的选择。 Numpy来计算, 和griddata用于将值附加到固定网格。
最后一点很重要,因为xy点的频率在我的数据中不是均匀分布的。首先,让我们从适合我的数据和任意网格大小的边界开始。原始数据的数据点也在这些x和y边界之外。
#determine grid boundaries
gridsize = 500
x_min = -8
x_max = 2.5
y_min = -2
y_max = 7
所以我们已经定义了一个在x和y的最小值和最大值之间有500像素的网格。
在我的数据中,在高度感兴趣的领域,有超过500个可用值;而在低兴趣区域,整个网格中甚至没有200个值;在x_min和x_max的图形边界之间就更少了。
因此,要得到一张漂亮的图片,任务就是求出高兴趣值的平均值,并填补其他地方的空白。
我现在定义我的网格。对于每一对xx-yy,我想有一个颜色。
xx = np.linspace(x_min, x_max, gridsize) # array of x values
yy = np.linspace(y_min, y_max, gridsize) # array of y values
grid = np.array(np.meshgrid(xx, yy.T))
grid = grid.reshape(2, grid.shape[1]*grid.shape[2]).T
为什么会有这么奇怪的形状?scipy。griddata需要一个(n, D)的形状。
Griddata通过预定义的方法计算网格中的每个点的值。 我选择“最近”-空网格点将被来自最近邻居的值填充。这看起来好像信息较少的区域有更大的细胞(即使事实并非如此)。人们可以选择插值“线性”,那么信息较少的区域看起来不那么清晰。这是品味问题,真的。
points = np.array([x, y]).T # because griddata wants it that way
z_grid2 = griddata(points, z, grid, method='nearest')
# you get a 1D vector as result. Reshape to picture format!
z_grid2 = z_grid2.reshape(xx.shape[0], yy.shape[0])
跳跃时,我们交给matplotlib来显示图
fig = plt.figure(1, figsize=(10, 10))
ax1 = fig.add_subplot(111)
ax1.imshow(z_grid2, extent=[x_min, x_max,y_min, y_max, ],
origin='lower', cmap=cm.magma)
ax1.set_title("SVC: empty spots filled by nearest neighbours")
ax1.set_xlabel('log gamma')
ax1.set_ylabel('log C')
plt.show()
在v型的尖端部分,你可以看到,我在寻找最佳点的过程中做了很多计算,而几乎所有其他地方的不太有趣的部分都有较低的分辨率。
编辑:为了更好地近似Alejandro的答案,请看下面。
我知道这是一个老问题,但想在Alejandro的回答中添加一些东西:如果你想要一个漂亮的平滑图像而不使用py-sphviewer,你可以使用np。Histogram2d,并对热图应用高斯滤波器(from scipy. nmage .filters):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.ndimage.filters import gaussian_filter
def myplot(x, y, s, bins=1000):
heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins)
heatmap = gaussian_filter(heatmap, sigma=s)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
return heatmap.T, extent
fig, axs = plt.subplots(2, 2)
# Generate some test data
x = np.random.randn(1000)
y = np.random.randn(1000)
sigmas = [0, 16, 32, 64]
for ax, s in zip(axs.flatten(), sigmas):
if s == 0:
ax.plot(x, y, 'k.', markersize=5)
ax.set_title("Scatter plot")
else:
img, extent = myplot(x, y, s)
ax.imshow(img, extent=extent, origin='lower', cmap=cm.jet)
ax.set_title("Smoothing with $\sigma$ = %d" % s)
plt.show()
生产:
Agape Gal'lo的散点图和s=16相互叠加(点击查看更好的视图):
我注意到我的高斯滤波方法和亚历杭德罗的方法的一个区别是,他的方法显示局部结构比我的好得多。因此,我在像素级上实现了一个简单的最近邻方法。该方法为每个像素计算数据中n个最近点距离的逆和。这种方法的分辨率很高,计算成本很高,我认为有更快的方法,所以如果你有任何改进,请告诉我。
更新:正如我所怀疑的,有一个更快的方法使用Scipy的Scipy . ckdtree。关于实现,请参阅Gabriel的回答。
总之,这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
def data_coord2view_coord(p, vlen, pmin, pmax):
dp = pmax - pmin
dv = (p - pmin) / dp * vlen
return dv
def nearest_neighbours(xs, ys, reso, n_neighbours):
im = np.zeros([reso, reso])
extent = [np.min(xs), np.max(xs), np.min(ys), np.max(ys)]
xv = data_coord2view_coord(xs, reso, extent[0], extent[1])
yv = data_coord2view_coord(ys, reso, extent[2], extent[3])
for x in range(reso):
for y in range(reso):
xp = (xv - x)
yp = (yv - y)
d = np.sqrt(xp**2 + yp**2)
im[y][x] = 1 / np.sum(d[np.argpartition(d.ravel(), n_neighbours)[:n_neighbours]])
return im, extent
n = 1000
xs = np.random.randn(n)
ys = np.random.randn(n)
resolution = 250
fig, axes = plt.subplots(2, 2)
for ax, neighbours in zip(axes.flatten(), [0, 16, 32, 64]):
if neighbours == 0:
ax.plot(xs, ys, 'k.', markersize=2)
ax.set_aspect('equal')
ax.set_title("Scatter Plot")
else:
im, extent = nearest_neighbours(xs, ys, resolution, neighbours)
ax.imshow(im, origin='lower', extent=extent, cmap=cm.jet)
ax.set_title("Smoothing over %d neighbours" % neighbours)
ax.set_xlim(extent[0], extent[1])
ax.set_ylim(extent[2], extent[3])
plt.show()
结果:
在Matplotlib词典,我认为你需要一个hexbin plot。
如果你不熟悉这种类型的图,它只是一个二元直方图,其中xy平面由一个规则的六边形网格镶嵌。
在直方图中,你可以数出每个六边形中的点的数量,将绘图区域离散化为一组窗口,将每个点分配给这些窗口中的一个;最后,将窗口映射到一个颜色数组上,你就得到了一个hexbin图。
虽然不像圆形或正方形那样常用,但直觉上,六边形是装箱容器的几何形状的更好选择:
六边形具有最近邻对称性(例如,方形容器没有, 例如,从正方形边界上的一点到另一点的距离 正方形内部并非处处相等)和 六边形是给出正平面的最高n多边形 镶嵌(例如,你可以安全地用六边形瓷砖重新设计厨房地板,因为当你完成时,瓷砖之间不会有任何空隙——而不是所有其他高n, n >= 7的多边形)。
(Matplotlib使用术语hexbin plot;所以(AFAIK)所有的绘图库的R;我仍然不知道这是否是这种类型的图表的普遍接受术语,尽管我怀疑它很可能是六角形装箱的缩写,这描述了准备数据显示的基本步骤。)
from matplotlib import pyplot as PLT
from matplotlib import cm as CM
from matplotlib import mlab as ML
import numpy as NP
n = 1e5
x = y = NP.linspace(-5, 5, 100)
X, Y = NP.meshgrid(x, y)
Z1 = ML.bivariate_normal(X, Y, 2, 2, 0, 0)
Z2 = ML.bivariate_normal(X, Y, 4, 1, 1, 1)
ZD = Z2 - Z1
x = X.ravel()
y = Y.ravel()
z = ZD.ravel()
gridsize=30
PLT.subplot(111)
# if 'bins=None', then color of each hexagon corresponds directly to its count
# 'C' is optional--it maps values to x-y coordinates; if 'C' is None (default) then
# the result is a pure 2D histogram
PLT.hexbin(x, y, C=z, gridsize=gridsize, cmap=CM.jet, bins=None)
PLT.axis([x.min(), x.max(), y.min(), y.max()])
cb = PLT.colorbar()
cb.set_label('mean value')
PLT.show()
这些解决方案都不适用于我的应用程序,所以我想出了这个解决方案。本质上,我在每个点上都放置了一个二维高斯分布:
import cv2
import numpy as np
import matplotlib.pyplot as plt
def getGaussian2D(ksize, sigma, norm=True):
oneD = cv2.getGaussianKernel(ksize=ksize, sigma=sigma)
twoD = np.outer(oneD.T, oneD)
return twoD / np.sum(twoD) if norm else twoD
def pt2heat(pts, shape, kernel=16, sigma=5):
heat = np.zeros(shape)
k = getGaussian2D(kernel, sigma)
for y,x in pts:
x, y = int(x), int(y)
for i in range(-kernel//2, kernel//2):
for j in range(-kernel//2, kernel//2):
if 0 <= x+i < shape[0] and 0 <= y+j < shape[1]:
heat[x+i, y+j] = heat[x+i, y+j] + k[i+kernel//2, j+kernel//2]
return heat
heat = pts2heat(pts, img.shape[:2])
plt.imshow(heat, cmap='heat')
以下是在相关图像上叠加的点,以及生成的热图: