python热图,Python实时变化热图绘制

I have a 2D grid 50*50. For each location I have an intensity value(i.e data is like (x,y,intensity) for each of those 50*50 locations). I would like to visualize the data as a heatmap.

The twist is that every second the intensity will change(for most of the locations), which means I will need to re-draw the heatmap every second. I am wondering what is the best library/approach to handle this kind of real-time varing heatmap.

解决方案

This really depends on how you get your data, but:

import matplotlib.pyplot as plt

import numpy as np

import time

# create the figure

fig = plt.figure()

ax = fig.add_subplot(111)

im = ax.imshow(np.random.random((50,50)))

plt.show(block=False)

# draw some data in loop

for i in range(10):

# wait for a second

time.sleep(1)

# replace the image contents

im.set_array(np.random.random((50,50)))

# redraw the figure

fig.canvas.draw()

This should draw 11 random 50x50 images with 1 second intervals. The essential part is im.set_array which replaces the image data and fig.canvas.draw which redraws the image onto the canvas.

If your data is really a list of points in the form (x, y, intensity), you can transform them into a numpy.array:

import numpy as np

# create an empty array (NaNs will be drawn transparent)

data = np.empty((50,50))

data[:,:] = np.nan

# ptlist is a list of (x, y, intensity) triplets

ptlist = np.array(ptlist)

data[ptlist[:,1].astype('int'), ptlist[:,0].astype('int')] = ptlist[:,2]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值