Showing posts with label statistic. Show all posts
Showing posts with label statistic. Show all posts

Saturday, May 19, 2012

How to pick out the maximum and minimum points when plotting with gnuplot?

Picking out the maximum and minimum points from a data file may be useful when you want to make some explanation to your graph. It is known that GPVAL_Y_MAX (GPVAL_Y_MIN) is the maximum (minimum) value. But what is the corresponding X coordinate?

With Gnuplot 4.6 the both the x and y coordinate of maximum and minimum points can be find out easily. The method is using new command "stats". This command is used for statistic. When it is run, some statistical results will be gotten. If your data file contains two column of data, (STATS_pos_max_y, STATS_max_y) will be the coordinate of the maximum point and (STATS_pos_min_y, STATS_min_y) will be the coordinate of the minimum point.

For example, we have a data file named "data.dat" like this

0.1     0.289010715
0.2     0.050630492
0.3     0.721247895
0.4     0.284271151
0.5     0.505051253
0.6     0.101819025
0.7     0.008466133
0.8     0.36249047
0.9     0.487576233
1.0     0.595090343
1.1     0.865255938
1.2     0.696628854
1.3     0.505899456
1.4     0.338131983
1.5     0.108034045

Run a gnuplot script as follows

reset
set term png
set output "max_min.png"
stats "data.dat" u 1:2 nooutput
set xrange [STATS_min_x:STATS_max_x]
set label 1 "Maximun" at STATS_pos_max_y, STATS_max_y offset 1,-0.5
set label 2 "Minimun" at STATS_pos_min_y, STATS_min_y offset 1,0.5
plot "data.dat" w p pt 3 lc rgb"#ff0000" notitle, \
     STATS_min_y w l lc rgb"#00ffff" notitle, \
     STATS_max_y w l lc rgb"#00ffff" notitle
set output

And you will get a graph like the following one.
Picking out the maximum and minimum points when plotting using gnuplot

Friday, September 16, 2011

Statistic analysis using gnuplot (1)

Last time we dealt with the maximum, minimum and mean value of a statistic analysis problem. Today the standard deviation will be added.
The standard deviation is defined as the square root of the variance. With variance stand for the mean squared value minus the squared mean value. The mean value have been dealt last time. We just need to store it. Strange it is, although there is only one data point after the smooth, when I plot it to a data file using the table mode, I get two output points with the second one marked with "u" which means undefined value.(who can tell me why?) In a similar way we plot the mean of squared value to another data file. Next we read this two value out from the data file. And use them to calculate the standard deviation. Since there is only two data points in the file, we use f(x)=ax+b to fit the data, and the fitted result will be exact, then we can use f(x) to get these exact values. At last we plot the standard deviation on the graph. The following is our final plotting script.
reset
plot "rand_t.dat" u 1:2	#To get the max and min value
ymax=GPVAL_DATA_Y_MAX
ymin=GPVAL_DATA_Y_MIN
ylen=ymax-ymin
xmax=GPVAL_DATA_X_MAX
xmin=GPVAL_DATA_X_MIN
xlen=xmax-xmin
set table "mean.txt"	#put the mean value out
plot "rand_t.dat" u ((xmax+xmin)/2.0):($2) smooth unique w p
unset table
set table "mean_squared.txt"	#put the mean of squared value out
plot "rand_t.dat" u ((xmax+xmin)/2.0):($2**2) smooth unique w p
unset table
f(x)=a*x+b	#The fit functions
g(x)=c*x+d
fit f(x) "mean.txt" u 0:2 via a,b	#Read the mean and mean of squared value
fit g(x) "mean_squared.txt" u 0:2 via c,d
mean=f(0)	#mean value
mean_squared=g(0)	#mean of the squared value
standard_deviation=sqrt(mean_squared-mean**2)	#standard deviation
print "The mean value is ",mean		#print the mean and standard deviation
print "The standard deviation is ",standard_deviation
#plot
set term post eps enhanced color lw 1.5 font ",20"
set output "statistic.eps"
set xrange [xmin:xmax]
set yrange [ymin-0.5*ylen:ymax+0.5*ylen]
set xlabel "time"
set ylabel "Random Signal"
#The labels
set label 1 at (xmin+xmax)/2.,ymax "Maximum" offset 0,0.5
set label 2 at (xmin+xmax)/2.,ymin "Minimum" offset 0,-0.5
set label 3 at (xmin+xmax)/2.,mean "Mean" offset 0,0.5
set label 4 at (xmin+xmax)/2.,mean+3*standard_deviation \
             "Mean+3{/Symbol \163}" offset 0,0.5
set label 5 at (xmin+xmax)/2.,mean-3*standard_deviation \
             "Mean-3{/Symbol \163}" offset 0,-0.5
plot "rand_t.dat" u 1:2 w p pt 7 ps 0.5 notitle,\
     mean w l lt 2 notitle "Mean",\
     ymax w l lt 3 notitle "Maximum",\
     ymin w l lt 3 notitle,\
     mean+3*standard_deviation w l lt 4 notitle,\
     mean-3*standard_deviation w l lt 4 notitle
#the six plot stand for raw data, mean, maximum, minimum,
#3-sigma upper line and 3-sigma lower line
At last we get figure like follows.

Statistic analysis using gnuplot

Wednesday, September 14, 2011

Statistic analysis using gnuplot (0)

I will talk about statistic analysis using gnuplot in this article. The following contents are covered---maximum, minimum, mean value, standard deviation.

We begin with the maximum and minimum as they are the simplest. There are two gnuplot defined variables (to see all the gnuplot variables use command "show variable all"), GPVAL_DATA_Y_MAX and GPVAL_DATA_Y_MIN. They are the maximum and minmum y value in the data file which you just used to plot. So after plotting you look over the gnuplot defined variables and you find the maximum and minimum values.

The mean value is a bit difficult. There is no such a gnuplot defined value. To find it out we will play a trick. There is smooth option called "unique" which makes the data monotonic in x and points with the same x-value are replaced by a single point having the average y-value. So it is appropriate for finding the mean value. We use command
plot "data_t.dat" u (constant-value):($n) smooth unique w point
to plot the mean value. Here constant-value is an arbitrary constant and $n is the column which contains the random data. Note that there is only one point plotted using the above command, so plot style line can not be used. To plot the the mean value using a line we may need to use xerrobars plot style. (Next time I will give a method to plot it using plot style line.)
plot "data_t.dat" u (constant-value):($n):(xerrobar-length) smooth unique w xerrorbars

Now we come to plot the data points, maximum, minimum and mean value to a picture. And standard deviation is left next time.
reset
plot "rand_t.dat" u 1:2	#To get the max and min value
ymax=GPVAL_DATA_Y_MAX
ymin=GPVAL_DATA_Y_MIN
ylen=ymax-ymin
xmax=GPVAL_DATA_X_MAX
xmin=GPVAL_DATA_X_MIN
xlen=xmax-xmin
#plot
set term png
set output "statistic.png"
set xrange [xmin:xmax]
set yrange [ymin-0.5*ylen:ymax+0.5*ylen]
set xlabel "time(ms)"
set ylabel "Random Signal(Arbitary Unit)"
plot "rand_t.dat" u 1:2 w p pt 7 ps 0.5 notitle,\
     "rand_t.dat" u (xmax+0.1*xlen):($2):(1.1*xlen)\
     smooth unique w xerrorbars notitle,\
     ymax w l lt 3 notitle,\
     ymin w l lt 3 notitle
#plot raw data, mean value, maximun and minimun respectively
#There is only one data point for the mean value. To make it 
#looks like a line, we plot it using a xerrorbars plot style.
rand_t.dat can be downloaded here. Picture statistic.png is shown below.
Statistic analysis using gnuplot--maximum,minimum and mean value

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.