我想找到 2 个有界区域。但是,我不知道蓝色曲线的功能,但我确实有它的数据表。我抬头发现了使用的可能性,scipy.integrate.simps
但我不知道如何指定该区域以红线(上或下)为界,但不以 x 轴为界。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,
AutoMinorLocator)
from sklearn.linear_model import LinearRegression
from scipy import interpolate
from scipy import integrate
%matplotlib inline
file = pd.read_excel("T8.xlsx","Phthalansäureanhydrid",usecols=[2,3])
X = file['Zeit(s)']
Y = file['Temperatur(Celcius Grad)']
fig, ax = plt.subplots()
ax.plot(X,Y,'-',color='#10A5F3', label="Phthalansäureanhydrid")
ax.grid(True, which='major', axis='both', color='#F19211', linestyle='-')
#ax.grid(True, which='minor', axis='both', color='#F19211', linestyle='--')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
#ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
#ax.legend(loc='upper center', frameon=True)
#major & minor ticks
ax.xaxis.set_major_locator(MultipleLocator(100))
ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
ax.xaxis.set_minor_locator(MultipleLocator(10))
#extrapolation - first line
temp1 = []
time1 = []
xnew1 = []
for i in file.index:
if i > 630:
temp1.append(file['Temperatur(Celcius Grad)'][i])
time1.append(file['Zeit(s)'][i])
else:
xnew1.append(file['Zeit(s)'][i])
extrapo1 = InterpolatedUnivariateSpline(time1, temp1, k=1)
ynew1 = extrapo1(xnew1)
#extrapolation - second line
temp2 = []
time2 = []
xnew2 = []
for i in file.index:
if 100<i<400:
temp2.append(file['Temperatur(Celcius Grad)'][i])
time2.append(file['Zeit(s)'][i])
if i>400:
xnew2.append(file['Zeit(s)'][i])
ynew2 = np.polyval(z,xnew2)
z = np.polyfit(time2,temp2,1)
#horizontal line
def hori(x):
a = xnew1.index(x)
b = xnew2.index(x)
return np.linspace(ynew2[b],ynew1[a])
#integral
plt.plot(xnew1,ynew1,'-',color='black')
plt.plot(xnew2,ynew2,'-',color='black')
plt.plot([470]*len(hori(470)),hori(470),'--',color='red')
plt.savefig('phth.pdf')
数据链接:https : //docs.google.com/spreadsheets/d/1xznXj-aA-Szq2s4KWb-qPWYxZbQNrA5FgUCQT6i7oVo/edit?usp=sharing
我将编写一个基本的说明性示例,以便您了解如何进行此类操作。这不适用于您的数据,但应该可以帮助您:
import matplotlib.pyplot as plt
import numpy as np
# Here are my curves
x = np.linspace(0, np.pi/2, num=100)
blue = np.sin(x)
orange = np.linspace(0.5, 0.8, num=100)
green = np.linspace(0.1, 0.2, num=100)
# Here is a plot to illustrate my example
plt.plot(x, blue, x, orange, x, green)
plt.show()
该图已用我想要解决的区域进行了注释;蓝色曲线下但以绿色和橙色曲线为界的面积:
因此,要计算出这个区域,我们将计算出以下区域:
当它高于绿色和低于橙色时,在蓝色曲线下
并将其添加到该区域
当它低于蓝色时,在橙色曲线下
然后减去面积
当它低于蓝色时在绿色曲线下
这有点复杂,但请尝试描绘这些区域,以便您了解我们添加和减去这些区域的原因。我们可以使用该np.trapz
函数并通过仅屏蔽我们想要的区域来计算出这些区域:
mask = (blue > green) * (blue < orange)
first_bullet_area = np.trapz(blue[mask], x[mask])
mask = (orange < blue)
second_bullet_area = np.trapz(orange[mask], x[mask])
mask = (green < blue)
third_bullet_area = np.trapz(green[mask], x[mask])
然后我们只需要做加法和减法:
>>> first_bullet_area + second_bullet_area - third_bullet_area
0.6190953349008973
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句