我有一个2D网格50 * 50。对于每个位置,我都有一个强度值(即数据类似(x,y,intensity)
,每个50 * 50个位置)。我想将数据可视化为热图。
扭曲是强度每秒都会改变(对于大多数位置),这意味着我需要每秒重新绘制热图。我想知道什么是处理这种实时变化热图的最佳库/方法。
答案 0 :(得分:5)
这实际上取决于您获取数据的方式,但是:
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()
这应该以1秒的间隔绘制11个随机50x50图像。重要的部分是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]
答案 1 :(得分:0)
谢谢你的回答,这对我有帮助。我现在可以补充一下,如果您希望它正确更新并以迭代方式显示在您的图中,您需要在最后添加一行:
fig.canvas.flush_events()
对于 Jupyter 用户,要在新窗口中打开一个图形,请在单元格的开头添加:
%matplotlib qt