計算下圖中折線與y軸圍成的面積,藍色為負,黃色為正,正負抵消後的和。
- 采用梯形面積和累加,
- 由於業務需要,本例以130為邊界,分別求50-130之間的面積,以及130-210之間的面積>
本例只給 ( 50 , x 1 ) , ( 70 , x 2 ) , ( 90 , x 3 ) , ( 130 , x 4 ) , ( 190 , x 5 ) , ( 230 , x 6 ) (50, x_1),(70, x_2),(90, x_3),(130, x_4),(190, x_5),(230, x_6) (50,x1),(70,x2),(90,x3),(130,x4),(190,x5),(230,x6),這 6 個點, 其中 x 1 = 0 x_1=0 x1=0
復雜寫法:
aera['down'] = df_10min_down.apply(lambda x: sum( \
[0.5*(abs(i[0][0])/(abs(i[0][0])+abs(i[0][1])))*i[1]*i[0][0]*(1-(i[0][1]/i[0][0])**2) if i[0][0]*i[0][1] < 0 else 0.5*(i[0][0]+i[0][1])*i[1] \
for i in zip(zip(x[:-1], x[1:]), height[:6])]), axis=1)
aera['up'] = df_10min_up.apply(lambda x: sum( \
[0.5*(abs(i[0][0])/(abs(i[0][0])+abs(i[0][1])))*i[1]*i[0][0]*(1-(i[0][1]/i[0][0])**2) if i[0][0]*i[0][1] < 0 else 0.5*(i[0][0]+i[0][1])*i[1] \
for i in zip(zip(x[:-1], x[1:]), height[6:])]), axis=1)
簡化寫法(推薦)
aera['down'] = df_10min_down.apply(lambda x: sum([0.5*(i[0][0]+i[0][1])*i[1] for i in zip(zip(x[:-1], x[1:]), height[:6])]), axis=1)
aera['up'] = df_10min_up.apply(lambda x: sum([0.5*(i[0][0]+i[0][1])*i[1] for i in zip(zip(x[:-1], x[1:]), height[6:])]), axis=1)