Python real time varying heat map plotting

后端 未结 1 1757
春和景丽
春和景丽 2021-01-03 09:06

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 visuali

相关标签:
1条回答
  • 2021-01-03 09:46

    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)
提交回复
热议问题