Python: Creating a 2D histogram from a numpy matrix

后端 未结 4 1834
清歌不尽
清歌不尽 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 15:50

    If you have the raw data from the counts, you could use plt.hexbin to create the plots for you (IMHO this is better than a square lattice): Adapted from the example of hexbin:

    import numpy as np
    import matplotlib.pyplot as plt
    
    n = 100000
    x = np.random.standard_normal(n)
    y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
    plt.hexbin(x,y)
    
    plt.show()
    

    enter image description here

    If you already have the Z-values in a matrix as you mention, just use plt.imshow or plt.matshow:

    XB = np.linspace(-1,1,20)
    YB = np.linspace(-1,1,20)
    X,Y = np.meshgrid(XB,YB)
    Z = np.exp(-(X**2+Y**2))
    plt.imshow(Z,interpolation='none')
    

    enter image description here

提交回复
热议问题