2017-12-11 8 views
-3

ich einen Teil oder die Daten darstellen möchtenPython Grundstück nur einen Teil der Daten

hier ist der Code

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from matplotlib import style 
... ... 
xs = [] 
avg = [] 
    for line in lines: 
     if len(line) > 1: 
      x, y1 = line.split(',') 
      xs.append(float(x)) 
      avg.append(float(y1)) 
ax1.plot(xs, avg, label='avg') 

ich einige der Code hinzugefügt, so dass Sie den Typ der Variablen sehen

ich habe versucht:

ax1.plot(xs[avg>0], avg[avg>0], label='avg') 

und funktioniert nicht

im Matlab würde ich irgendeine Sache tun:

Indxs=find (ys>0) 
Plot(xs(indxs),ys(indxs)) 
+0

Sie bei numpy.where aussehen kann (https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.where.html) um die Indizes der gewünschten Teilmenge zu finden. – KPLauritzen

Antwort

1

Die Syntax korrekt ist. Das Problem ist, dass xs und avg keine Nummernfelder sind. Sie müssen also zuerst diese Listen in num- py Arrays konvertieren, dann wird das Slicing wie erwartet funktionieren.

1

Was Sie nicht tun, da Ihr Index (Durchschnitt> 0) in Python ein Boolean ist. Wenn Sie mit Matlab vertraut sind, sollten Sie auf jeden Fall numpy Boolean Indizierung versuchen.

Sie tun können:

import numpy as np 
xs = numpy.asarray(x) 
ys = numpy.asarray(y) 
ys_filtered = ys[x > 0]