在2d直方图中没有出现点

时间:2016-10-23 15:51:18

标签: python matplotlib histogram

我一直在尝试将数组中的数据输入到2d直方图中,并使用plt.imshow来显示它。但到目前为止我还没有成功。我得到一个带有正确标签的空数组,但没有要检测的点。我在网上查了一些例子,但没有用。

    d[0]= array([ 559.31299349,  507.44063212,  596.05952403, ...,  531.48861237,
    525.03097371,  512.51860453])
    d[1]= array([ 604.44753343,  513.26418859,  658.79946406, ...,     543.09749822,
    522.69953756,  579.40805154])


    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline

    d = np.load('XandY.npy')

    x = d[0]
    y = d[1]

    gridx = np.linspace(min(x),max(x),10)
    gridy = np.linspace(min(y),max(y),10)

    H, xedges, yedges = np.histogram2d(x, y, bins=[gridx, gridy])

    fig1 = plt.figure()
    plt.plot=(x,y,'ro')
    plt.grid(True)
    plt.xlabel('array X')
    plt.ylabel('array y')

    plt.figure()
    myextent  =[xedges[0],xedges[-1],yedges[0],yedges[-1]]
    plt.imshow(H.T,origin='low',extent=myextent,aspect='auto')
    plt.plot(x,y,'ro')
    plt.colorbar()

    plt.show()

我的观点何去何从?

1 个答案:

答案 0 :(得分:0)

以下简化代码对我有用。

def main():
    #output image
    outpath=os.path.join('data', 'matplot_hist2d_example.png')

    #get 100 random scatter points in the range(500.0-700.0)
    np.random.seed(1702)
    rand_pts=np.random.uniform(low=500.0, high=700.0, size=(100,2))
    x = rand_pts[:, 0]
    y = rand_pts[:, 1]
    #ensure 10 bins along each axis
    gridx = np.linspace(min(x), max(x), 11)
    gridy = np.linspace(min(y), max(y), 11)

    #histogram 2d
    H, xedges, yedges = np.histogram2d(x, y, bins=[gridx, gridy])

    #plotting
    fig1 = plt.figure()
    plt.xlabel('array X')
    plt.ylabel('array Y')
    myextent  =[xedges[0],xedges[-1],yedges[0],yedges[-1]]
    plt.imshow(H.T, origin='low', extent=myextent,aspect='auto')
    plt.colorbar()
    #show points as well
    plt.scatter(x,y)
    plt.show()

    #save
    fig1.savefig(outpath)
    plt.close(fig1)
    pass

请参阅下面的结果 result of plot