Posts Tagged ‘graph’

Finding the area under a curve with a spreadsheet program (Excel, Libreoffice Calc) or QtiPlot

Tuesday, October 28th, 2014

For performing elemental analysis by x-ray photoelectron spectroscopy, I needed to integrate and find the area under the peaks in some spectra. One approach is to fit each peak with a Gaussian function and use the area of the Gaussian. For some convoluted peaks, I wanted something simpler. I wanted to get the total area under each peak without fitting it in any way.

I use QtiPlot almost exclusively for plotting and fitting (fityk is my other go-to program) and one of the menu options for plots is Analysis->Integrate. That option brings up a tiny dialog where you can select the curve and range over which to integrate.

Screenshot - 10282014 - 09:13:32 PM

In the results log, an area is printed for the integrated curve. I wasn’t sure what QtiPlot was doing so I did an experiment with a simple set of data. I now know conclusively the algorithm used by QtiPlot and am able to replicate it in a spreadsheet program. I use LibreOffice Calc, but the instructions here should work in Excel as well.

Given a set of n points, P1 through Pn, a curve is generated by connecting each point with straight lines.qtiplot-integration-plot1

The area under the curve between two points, (x1, y1) and (x2, y2), is a trapezoid with area A = 1/2 |x1 – x2| (y1 + y2).qtiplot-integration-plot2

To find the area under the curve defined by a set of points, QtiPlot iterates through the points from P0 to Pn-1. For each point Pi, it calculates the area using Pi as (x1, y1) and Pi+1 as (x2, y2) and sums all of the trapezoid areas.

The above process is easy to replicate in LibreOffice Calc (or Excel) with the x,y data that define the curve. Assuming the x coordinates are in column A starting at row 1 and the y coordinates are in column B starting row 1, the areas of the individual trapezoids can be generated in column C. The formula in the first row is: “=0.5*abs(A1-A2)*(B1+B2)”. That formula is then copied down column C in all of the rows but the last. Finally, the areas are summed to give the total area under the curve.

Screenshot - 10282014 - 09:48:15 PM

Extract Y at X – A QtiPlot plug-in/script

Saturday, September 7th, 2013

I’ve written and posted a QtiPlot plug-in/script (python) I call “Extract Y at X” that extracts the y-values at a given x-value for a series of curves contained in a graph and generates a new table with those values.

The files can be downloaded using the link below. The .zip file contains three files: the script (.py) file, an xml file defining the Qt inteface (.ui), and a README file. Refer to the README file for notes on installation and use.

I wrote this script to analyze a large group of datasets that I had collected. Each dataset was measurement of current as a function of voltage for a device I had constructed and each was collected at a different temperature. In a single dataset the current grew exponentially and across datasets the current grew exponentially with temperature. In order to quantitatively determine how the current changed with temperature I needed to extract the current at some voltage from each dataset. The usual way to do this is to look through the data manually in each dataset and find the point I needed, e.g. the current at +1 V for each dataset. Given that I had so many datasets, it was useful to write a script to do this for me.


Update 7/18/14:

I found and fixed a bug that limited the number of extracted points to 30.

Update  8/5/14:

As described in another post this plug-in may not work in Windows versions of QtiPlot without the proper dependencies satisfied. The trouble is not the script itself but its reliance on the uic module of Qt which may not have been packed with a particular QtiPlot version when it was compiled and is therefore unavailable to the script. Linux users should be fine, so long as Qt libraries are installed.