我是Python初学者。
曲线(x,y)有两个数据文件:https://drive.google.com/open?id=1ZB39G3SmtamjVjmLzkC2JefloZ9iShpO
黑色区域(A)和红色区域(B)
我只知道如何找到总面积:
from scipy.integrate import trapz
with open('./x_data.txt', 'rt') as f:
x_file = f.read()
with open('./y_data.txt', 'rt') as f:
y_file = f.read()
xlist = []
for line in x_file.split('\n'):
if line:
xlist.append(float(line.strip()))
ylist = []
for line in y_file.split('\n'):
if line:
ylist.append(float(line.strip()))
if len(xlist) != len(ylist):
print(len(xlist), len(ylist))
raise Exception('X and Y have different length')
xData = np.array(xlist)
yData = np.array(ylist)
area = trapz(y = yData, x = xData)
print("area =", area)
您可以使用Simpsons规则或Trapezium规则来以给定的y值表以规则的间隔来计算图形下的面积。
from scipy.integrate import simps
from numpy import trapz
参考:Calculating the area under a curve given a set of coordinates, without knowing the function