2017-03-22 5 views
2

Das ist peinlich, aber ich bin nicht in der Lage, dies plotten:Plotten matplotlib von Nump ndarray

import numpy as np 
    import matplotlib.pyplot as plt 
    datf=np.loadtxt(filename, dtype=float,delimiter=" ") 
    print((datf)) 
    plt.plot(datf[:0], datf[:1]) 
    plt.show() 

Dies ist datf:

[[ 1.   19.778986 ] 
[ 1.3625678 -1.9363698] 
[ 1.4142136 6.5144132] 
[ 1.6901453 3.8092139] 
[ 2.   -4.0222051]] 

Und der Fehler ist:

ValueError: x and y must have same first dimension 

Antwort

2

Es sieht so aus, als ob Sie versuchen, die erste Spalte als x und die zweite Spalte als y darzustellen. Sie haben bei der Indexierung einen Fehler gemacht. Um die erste Spalte von datf zu erhalten, müssen Sie datf[:, 0] tun (beachten Sie das Komma).

Ihre endgültige Code wird wie folgt aussehen:

import numpy as np 
    import matplotlib.pyplot as plt 
    datf=np.loadtxt(filename, dtype=float,delimiter=" ") 
    print((datf)) 
    plt.plot(datf[:, 0], datf[:, 1]) # note the commas here 
    plt.show() 
+0

Vielen Dank sein. Ich habe das vermisst und hatte keine Ahnung, warum diese Kleinigkeit nicht funktioniert – BaRud

0

die 1. und 2. Säule zu erhalten, müssen die Indizierung

plt.plot(datf[:,0],datf[:,1]) 
Verwandte Themen