문제

일반 데이터 의 일부를 찍고 전원 값에서 DB 값으로 변환합니다. 시스템으로 인해 이러한 값은 '여기에서 유용한 데이터가 끝나는 것'으로 0으로 사용됩니다 (정의 된 값이 아닌 수학의 성격).

이들을 다루는 평소의 방법은 시도 / 제외하고 기본 '낮은'값, 예 : 를 반환하는 것에 대한 변환을 포장합니다.

def f(value):
    try:
        return convert(value)
    except ValueError:
        return -140 #implementation specific, don't worry
.

이것은 괜찮습니다. 그래프로 인해 제이자가 내 프레임 워크 내에서 사용의 90 %를 사용합니다.

나는 게으른 일이므로 내가하는 일은 다음과 같습니다 :

pl.plot(xvals,map(f,yvals))
.

이것은 데이터를 올바르게 끌어 올 때 데이터가 종료되면 예상되는 동작 인 절벽을 뽑습니다. 그러나 제가 일어나고 싶은 것은 End End 에 맞추고 F ()을 모두로 멀리하는 것입니다.

루프로 맵을 깨는 것 이외에 누구나 훌륭한 아이디어를 얻었습니까?

업데이트 :

나는 pylab / matplotlib을 사용하고 있습니다 "끝점"은 실행에 따라 다릅니다. 때로는 위의 것들이 '나쁜'값이 없으므로 atall에서 중요하지 않습니다. 이것은 모두 나에게 게으르기를 위해 모든 노력으로, YDATA의 최소 (atm, ylim (-140),이 경우에, ylim (-140) 단지 ylim (-140)을 기반으로 동적 YLIM을 재설정하는 대신 MATPLOTLIBS 그래프 스케일링을 사용합니다.)

답변에 대한 막연하게 중요한 업데이트 : Unutbu의 답변은 정기적으로 사용되는 기능에서 정기적으로 사용되는 기능에서의 정지를 끌어 올리고있는 것처럼 (질문의 종속성에서 언급하지 않은) 내 구현으로 인해 실제로 사용할 수 있습니다. Trude-Except에서 다른 경우의 다른 경우; 때때로 -inf는 당신이 생각하는 것보다 더 의미가 있습니다.

모든 사람들에게 굉장하게 빠르게 감사 드리며 Quyutbu에 대한 Unutbu로 사과드립니다.

도움이 되었습니까?

해결책

Perhaps there's some trick in the plotting library, but a much better options seems not generating such data to begin with. It's not that map saves you thirty lines of code...

Use itertools.takewhile(lambda y: y != NO_VALUE, (f(y) for y in yvals)) (and wrap it in a call to list if the plotting library requires a list instead of an iterable).

Edit: I had an even better idea: In the wrapper, add

except ValueError:
    raise StopIteration

That's the exception signaling "end of iterale", and map respects it.

다른 팁

If you are using matplotlib, then it implies you have numpy installed.

Since you are converting to dB, it sounds like you might be taking a log. In that case, np.log(0) = -inf.

You can mask nans and infs with the numpy function np.ma.masked_invalid, and matplotlib can plot masked arrays. For example,

import matplotlib.pyplot as plt
import numpy as np

xvals=np.arange(100)
yvals=np.cumsum(np.random.random(100))
yvals[-10:]=0
yvals=np.log(yvals)

print(yvals[-10:])
# [-Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf]

yvals=np.ma.masked_invalid(yvals)
plt.plot(xvals,yvals)
plt.show()

yields enter image description here

Notice that the graph ends with xval equal to 89, since the last 10 values of yval are masked.

You're needlessly limiting yourself by refusing to use a looping construct.

In your situation you want to stop iterating over data when a certain value is reached, that is exactly the purpose of forloops and breaks

yvals_ = []
for y in yvals:
    y_ = f(y)
    if y_ == -140:
        break
    else:
        yvals_.append(y_)

p1.plot(xvals[:len(yvals_)],yvals_)

It seems like you have data, and you don't want to plot the last point. So what about not plotting it?

pl.plot(xvals[:-1], map(f, yvals)[:-1])
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top