Tuesday, September 20, 2011

Gnuplot I/O: Use fit command to scanf data from files

We usually record our data as follows:
x0         y0
x0+dx      y1
x0+2*dx    y2
In fact there is so much redundancy. For the first column we only need to record two numbers x0 and dx. So in a compact format the data can be stored as:
Some one may say "these days the memory is so cheap, I do not care the bytes". But have you consider the speed? When you come to process the data, the larger the data file is, the longer time will your program takes. So this new format is worthy.

But problems raised when you want to plot the data file using gnuplot. The software do not recognize this new format. So the task of this article is making gnuplot understand this format.

Firstly we need to read x0 and dx out form the file. As for gnupot there is no functions like scanf in c programming language, we need to play a trick here. We use command fit to read the data out. We known that if there is only two point, the fitting result using function f(x)=a*x+b will be exact. So we use command
fit f(x) "my_format.dat" u 0:1 every ::0::1 via a,b
to fit the first two points. Then we get x0=f(0), dx=f(1). Now the xvalue can be generated by x0+$0*dx. As the xvalue has been prepared, we can come to plot the data file. Now Let me show an example. The plotting script reads:
set term png font "Times,18"
set output "my_format.png"
fit f(x) "my_format.dat" u 0:1 every ::0::1 via a,b
set xlabel "Time(ms)"
set ylabel "Light Intensity(Arbitary Unit)"
set mxtics 5
set mytics 2
set grid xtics mxtics ytics mytics
set offset 0,0,0.05,0
plot "my_format.dat" u (x0+$0*dx):1 every ::2 with lines linewidth 2 notitle
Data file my_format.dat can be downloaded here. And the graph is shown below.

No comments:

Post a Comment

Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.