import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.cm as c
第一种:直接构造
- 直接用数组构造二维的高斯分布
grid_x = np.arange(40)
grid_y = np.arange(40)
[X,Y] = np.meshgrid(grid_x,grid_y)
point = [33,26]
单维的高斯分布
'''point 是生成高斯分布的中心点
X 代表图上所有点的 x 坐标
Y 代表图上所有点的 y 坐标
根据 numpy 的广播特性,可以直接通过下列式子
得到每个 x-u 和每个 y-u 的值
'''
X_ = X - point[0]
Y_ = Y - point[1]
d = X_ ** 2 + Y_ ** 2
sigma = 7
sigma_square = 2 * sigma * sigma
E = d / sigma_square
graph = np.exp(-E)
plt.imshow(graph,cmap=c.jet)
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, graph, cmap=c.jet, linewidth=0.1,rstride=1, cstride=1)
fig.colorbar(surf, shrink=.7, aspect=20)
ax.contourf(X,Y,graph,zdir='z',offset=-5)
ax.contourf(X,Y,graph,zdir='x',offset=-2)
ax.contourf(X,Y,graph,zdir='y',offset= 40)
plt.show()
第二种:scipy + opencv
from scipy.ndimage import gaussian_filter
import cv2
heatmap = np.zeros((40,40))
plt.imshow(heatmap)
point = [33,26]
cv2.circle(heatmap, (point[0], point[1]), 1 , 1, cv2.FILLED)
array([[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., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]])
plt.imshow(heatmap)
heatmap = gaussian_filter(heatmap, 7)
plt.imshow(heatmap,c.jet)
maxi = np.amax(heatmap)
heatmap = heatmap / maxi
plt.imshow(heatmap,c.jet)
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, heatmap, cmap=c.jet, linewidth=0.1,rstride=1, cstride=1)
fig.colorbar(surf, shrink=.7, aspect=20)
ax.contourf(X,Y,heatmap,zdir='z',offset=-5)
ax.contourf(X,Y,heatmap,zdir='x',offset=-2)
ax.contourf(X,Y,heatmap,zdir='y',offset= 40)
plt.show()
第三种:产生向量热图
another_point = [10,17]
vector_heatmap = np.zeros((40,40))
cv2.line(vector_heatmap, (point[0],point[1]),(another_point[0],another_point[1]),1,1)
vector_heatmap = gaussian_filter(vector_heatmap, 3)
maxi = np.amax(vector_heatmap)
vector_heatmap = vector_heatmap / maxi
plt.imshow(vector_heatmap,c.jet)
vector_heatmap[20,20]
0.9567312060112102
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, vector_heatmap, cmap=c.jet, linewidth=0.1,rstride=1, cstride=1)
fig.colorbar(surf, shrink=.7, aspect=20)
ax.contourf(X,Y,vector_heatmap,zdir='z',offset=-2)
ax.contourf(X,Y,vector_heatmap,zdir='x',offset=-2)
ax.contourf(X,Y,vector_heatmap,zdir='y',offset= 40)
ax.set_zlim(-2,1)
# 设置图像z轴的显示范围,x、y轴设置方式相同
plt.show()