matplotlib学习之:三种产生 heatmap 热图的可视化方式

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()

在这里插入图片描述

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暖仔会飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值