问题描述
我有一个2D网格50 * 50。对于每个位置,我有一个强度值(即数据类似于(x,y,强度)
,每个50 * 50个位置)。我希望将数据可视化为热图。
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()
这应该随机抽取11 50x50张图像,间隔为1秒。重要的部分是 im.set_array
替换图像数据和 fig.canvas.draw
,它将图像重新绘制到canvas。
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.
如果您的数据确实是形式的点列表(x,y ,强度)
,您可以将它们转换为 numpy.array
:
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]
这篇关于Python实时变化热图绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!