[英]How to find the area under the curve
I am a beginner Python user.我是 Python 初学者。
There are two data files for the curve (x, y): https://drive.google.com/open?id=1ZB39G3SmtamjVjmLzkC2JefloZ9iShpO曲线 (x, y) 有两个数据文件: https : //drive.google.com/open?id=1ZB39G3SmtamjVjmLzkC2JefloZ9iShpO
How to find two areas under the curve, as shown in Figure:如何求曲线下的两个区域,如图:
Black area (A) and red area (B)黑色区域(A)和红色区域(B)
I`m only know to how find total area:我只知道如何找到总面积:
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)
You can use Simpsons rule or the Trapezium rule to calculate the area under a graph given a table of y-values at a regular interval.您可以使用辛普森规则或梯形规则来计算图形下的面积,给定一个定期间隔的 y 值表。
from scipy.integrate import simps
from numpy import trapz
reference: Calculating the area under a curve given a set of coordinates, without knowing the function参考: 在不知道函数的情况下计算给定一组坐标的曲线下面积
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.