Python:计算曲线下的面积

时间:2021-06-26 11:33:26

标签: python pandas scipy integral

我有带有 2 列“x”、“y”的 Pandas DataFrame:

internal Dictionary<string, string> SplitTextByHtmlTags(string input)
{
    var result = new Dictionary<string, string>();

    //  Iterating through a string
    for (var i = 0; i < input.Length; i++)
    {
        //  Detecting the opening of the tag
        if (input[i] == '<')
        {
            string 
                tag = "",       //  Name of the tag
                content = "";   //  Content of the tag

            //  Iterating over the tag
            for (int j = i+1; j < input.Length; j++)
            {
                /**
                    * If alphabetic characters are being iterated over,
                    * then, most likely, this is the name of the tag.
                    */
                if (!input[j].IsLetter())
                {
                    //  As soon as any character that is not a letter occurs
                    for (int k = j; k < input.Length; k++)
                    {
                        //  Looking for the end of the tag
                        if (input[k] == '>')
                        {
                            //  Sorting through the contents of the tag
                            for (int l = k+1; l < input.Length; l++)
                            {
                                if (input[l] != '<')
                                {
                                    content += input[l];

                                    /*
                                    * We move the "cursor" of the main loop
                                    * to the place where the tag opening symbol was found.
                                    */
                                    i = l;

                                    //  We put the found values in the map
                                    result.Add(tag, content);

                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
                else tag += input[j];
            }
        }
    }

    return result;
}

这是剧情:

enter image description here

我想分别获得高于 y=0 和低于 y=0 的三角形区域。

有没有简单的方法可以做到这一点?

我尝试使用集成,但似乎我做错了什么:

tmp = pd.DataFrame()
tmp['x'] = [1, 2, 5, 9, 12, 14]
tmp['y'] = [0, 1, -2, 2, -1, 1] 
tmp.plot(x = 'x', y = 'y')

ax = plt.gca()
ax.set_aspect('equal')
ax.grid(True, which='both')


ax.axhline(y=0, color='k')
ax.axvline(x=0, color='k')
plt.show()

这是一个更小/更简单的代码,用于显示我想要做什么。其实我的数据是相当大的。 我想我可以通过在 y = 0 处添加相应的 (x, y) 值来达到预期的结果,但我想知道是否存在可以完成类似工作的现有函数。

1 个答案:

答案 0 :(得分:1)

我希望你所说的面积是指 x 轴和曲线之间的面积,因为不可能有开放曲线的面积,所以基于这个假设,我还假设 x 始终是一个递增的数字
如果您检查 pos 和 neg 数据框,它们会变成与您想要的完全不同的形状。要计算面积,您需要分别计算x轴上下所有图形的面积,即识别所有x交点并找到交点之间的面积

我已经编写了一个通用代码,您仍然需要添加边缘情况,其中信号不是作为拦截开始或结束

tmp['change'] = tmp['y']*tmp['y'].shift(1)<0 ## points before which intercept would be found
tmp['slope']=(tmp['y']-tmp['y'].shift(1))/(tmp['x']-tmp['x'].shift(1))  ## identify slope
tmp['intersection']=-tmp['y']/tmp['slope']+tmp['x']  ## identify point of intersection
intersections=tmp[tmp['change']==True]  ## only take intersection from points where sign of 'y has changed

intersections=intersections[['intersection']]
intersections.rename(columns={"intersection":"x"}, inplace=True)
intersections['y']=int(0)

tmp = tmp[['x','y']]
tmp=tmp.append(intersections)
tmp=tmp.sort_values(by='x')
tmp=tmp.reset_index(drop=True)

crossing = tmp[tmp['y']==0].index  ## points between which area is to be identified
area=0
for i in range(len(crossing)-1):
    area_tmp=integrate.trapz(tmp[crossing[i]:crossing[i+1]+1]['y'],tmp[crossing[i]:crossing[i+1]+1]['x'])
    area+=abs(area_tmp)
    # print(area_tmp)
print(area)

这给出了 10 的答案,您仍然需要为最后一个三角形添加边缘情况

P.S:无法评论问题