Python: Creating a 2D histogram from a numpy matrix

后端 未结 4 1741
清歌不尽
清歌不尽 2020-12-25 15:31

I\'m new to python.

I have a numpy matrix, of dimensions 42x42, with values in the range 0-996. I want to create a 2D histogram using this data. I\'ve been looking a

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-25 16:00

    @unutbu's answer contains a mistake: xidx and yidx are calculated the wrong way (at least on my data sample). The correct way should be:

    xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1)
    yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1)
    

    As the return dimension of np.digitize that we are interested in is between 1 and len(xedges) - 1, but the c = hist[xidx, yidx] needs indices between 0 and hist.shape - 1.


    Below is the comparison of results. As you can see you get similar but not the same result.

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax1 = fig.add_subplot(211)
    ax2 = fig.add_subplot(212)
    
    n = 10000
    x = np.random.standard_normal(n)
    y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
    xedges, yedges = np.linspace(-4, 4, 42), np.linspace(-25, 25, 42)
    hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges))
    
    xidx = np.clip(np.digitize(x, xedges), 0, hist.shape[0] - 1)
    yidx = np.clip(np.digitize(y, yedges), 0, hist.shape[1] - 1)
    c = hist[xidx, yidx]
    old = ax1.scatter(x, y, c=c, cmap='jet')
    
    xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1)
    yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1)
    
    c = hist[xidx, yidx]
    new = ax2.scatter(x, y, c=c, cmap='jet')
    
    
    plt.show()
    

提交回复
热议问题