2016-10-23 2 views
0

Ich habe versucht, Daten von Arrays in ein 2D-Histogramm einzugeben und plt.imshow zu verwenden, um es anzuzeigen. Allerdings war ich bisher nicht erfolgreich. Ich bekomme ein leeres Array mit den richtigen Labels, aber es gibt keine Punkte zu entdecken. Ich habe online Beispiele gesucht, ohne Erfolg.Keine Punkte im 2d-Histogramm

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() 

Wo sind meine Punkte hin?

Antwort

0

Der folgende vereinfachte Code funktionierte für mich.

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 

Siehe die Ergebnisse unter result of plot

Verwandte Themen