Generate a heatmap in MatPlotLib using a scatter data set

后端 未结 12 2137
南方客
南方客 2020-11-22 09:22

I have a set of X,Y data points (about 10k) that are easy to plot as a scatter plot but that I would like to represent as a heatmap.

I looked through the examples in

12条回答
  •  逝去的感伤
    2020-11-22 10:19

    If you don't want hexagons, you can use numpy's histogram2d function:

    import numpy as np
    import numpy.random
    import matplotlib.pyplot as plt
    
    # Generate some test data
    x = np.random.randn(8873)
    y = np.random.randn(8873)
    
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
    
    plt.clf()
    plt.imshow(heatmap.T, extent=extent, origin='lower')
    plt.show()
    

    This makes a 50x50 heatmap. If you want, say, 512x384, you can put bins=(512, 384) in the call to histogram2d.

    Example: Matplotlib heat map example

提交回复
热议问题