Python实时变化热图绘图

aha*_*aha 3 python matplotlib ipython matplotlib-basemap

我有一个2D网格50 * 50。对于每个位置,我都有一个强度值(即,数据类似于(x,y,intensity)那50 * 50个位置中的每个位置)。我想将数据可视化为热图。

扭曲之处在于(对于大多数位置而言)强度每秒都会变化,这意味着我需要每秒重新绘制热图。我想知道什么是最好的库/方法来处理这种实时变化热图。

DrV*_*DrV 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()
Run Code Online (Sandbox Code Playgroud)

这应该以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]
Run Code Online (Sandbox Code Playgroud)