我有一个2D网格50*50。对于每个位置,我有一个强度值(也就是说,对于这50*50个位置中的每一个,数据类似于(x,y,intensity) )。我想把数据可视化为热图。
扭曲的是,每一秒强度都会变化(对于大多数位置),这意味着我需要每秒钟重新绘制热图。我想知道处理这种实时热图的最好的库/方法是什么。
发布于 2014-08-19 18:10:15
这取决于您如何获取数据,但是:
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将图像重绘到画布上。
如果数据实际上是表单(x, y, intensity)中的点列表,则可以将它们转换为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]发布于 2021-04-29 13:25:03
谢谢你的回答,这确实对我有帮助。现在请允许我补充一下,如果您想要以迭代的方式正确地更新和显示您的图形,则需要在末尾添加一行:
fig.canvas.flush_events()对于木星用户来说,要在新窗口中打开的图形在单元格的开头添加:
%matplotlib qthttps://stackoverflow.com/questions/25385216
复制相似问题