asked    Merry     2018-10-22       python       55 view        3 Answers

[SOLVED] Calculate the area under each peak in a graph in python

I am trying to calculate the area under each peak in a graph that I plotted with a set of x and y co-ordinates,

I don't have a function for (x,y), and so I haven't been able to find an appropriate method to do the same.

the co-ordinates are

{
 [10 10]
 [11  1]
 [12  7]
 [14  4]
 [16  8]
 [17  5]]}

And y=0 for all the unmarked x values

The graph plotted with the (x,y) co-ordinates

  3 Answers  

        answered    Jeff     2018-10-22      

The area below two points consists of a 'square' part and a triangular part. The trick is to calculate them both separately and add them. Do this for all points, and you get a list of areas under peaks:

points = [[10, 10],
          [11, 1],
          [12, 7],
          [14, 4],
          [16, 8],
          [17, 5]]

xs = [val[0] for val in points]

areas = []
for i in range(-1, points[-1][0] - points[0][0] + 1):
    cur_x = points[0][0] + i
    cur_y = 0 if cur_x not in xs else next(point[1] for point in points if point[0] == cur_x)
    next_x = cur_x + 1
    next_y = 0 if next_x not in xs else next(point[1] for point in points if point[0] == next_x)


    square = min(cur_y, next_y)
    triangle = (cur_y + next_y) / 2
    areas.append(square + triangle)

print(areas)
> [5.0, 6.5, 5.0, 3.5, 2.0, 2.0, 4.0, 11.5, 2.5]


        answered    Agnes     2018-10-22      

A bit simple and correct?

points = [[10, 10],
          [11, 1],
          [12, 7],
          [14, 4],
          [16, 8],
          [17, 5]]

areas = []
areas.append( points[0][0]/2.0 )

for i in range(0, points[-1][0] - points[0][0]-2):

    if ( points[i+1][0] == points[i][0]+1 ):
        areas.append( (points[i+1][1] + points[i][1] )/2.0)
    elif ( points[i+1][0] >= points[i][0]+2):
        areas.append( (points[i][1] )/2.0)
        areas.append( (points[i+1][1] )/2.0)

areas.append( points[-1][1]/2.0 )    

print(areas)
>[5.0, 5.5, 4.0, 3.5, 2.0, 2.0, 4.0, 6.5, 2.5]


        answered    Rita     2018-10-22      

You have to process one by one for each trapezoid.

Area_1 = ( y1 + y2 ) * 1 / 2

example : (10 + 1 ) * 1 / 2





Your Answer





 2018-10-22         Sid

SpringMVC built by IDEA: No converter found for return value of type

I use IDEA to build a simple RESTful server based on SpringMVC. But @RestController and @ResponseBody can't convert POJO to JSON by jackson JSON.In dispatcher-servlet.xml:<mvc:annotation-driven/>In pom.xml: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> ...
 java                     2 answers                     34 view
 2018-10-22         Dawn

How do I close the download pop in chrome browser when downloading PDF

I am using java selenium to automate my application.I am downloading PDF and parsing.There are multiple test cases with PDF in my application. So once i download first PDF the chrome show a pop at the bottom of browser that file is downloaded with file name so next time when i try to download the second PDF the Download button is below this pop and not able to click on download option.Used isDisplayed method to check status which return false and isEnabled returns true.Tried execution of the PDF testcase in another window it is not working either. Please refer the image for...
 java                     2 answers                     32 view
 2018-10-22         Walter

Array of Bitmap from array in xml file

I have in my array.xml an array of more than 100 images with all different name.<array name="icons"> <item>@drawable/setting</item> <item>@drawable/simplemode</item>...I need to load all them inside an array of BitmapHow can I do? Thanks Resources arrays that contains references to Drawables are called TypedArray in Android. You can refer to them very easily. Here's an example:TypedArray icons = getResources().obtainTypedArray(R.array.icons);Not, icons can retrieve the drawables for you. You can find the reference here: https:/...
 android                     1 answers                     37 view