Pierre
Pierre

Reputation: 490

How to get the average number of data points?

Here is my data file :

12100 4592

14400 5000

16900 6443

12100 4479

14400 5393

16900 5969

12100 4605

14400 5353

16900 6268

I would like to have the average of 3 tests and draw a line. For example, I want to draw these 3 points with gnuplot:

12100 4558,66

14400 5248

16900 6226,66

Where the second value is the average of the 3 tests.

Upvotes: 0

Views: 1691

Answers (2)

theozh
theozh

Reputation: 26198

The short answer would be using smooth unique (check help smooth unique)

plot FILE u 1:2 smooth unique

The smooth unique option makes the data monotonic in x; points with the same x-value are replaced by a single point having the average y-value. The resulting points are then connected by straight line segments.

With this, you are independent of varying numbers of identical x-values.

The option smooth unique exists at least since gnuplot 4.2.6 (2009), probably much earlier.

The following example plots the data, the average and the average values as labels. Since it uses datablocks, it requires gnuplot 5.0. For gnuplot 4.x versions you would have to read/write data from/to datafile, but unfortunately, there is a "bug" writing an additional number to the file, which is unwanted when plotting the labels.

Script: (works for gnuplot>=5.0)

### plot average of data
reset session

$Data <<EOD
12100 4592
14400 5000
16900 6443
12100 4479
14400 5393
16900 5969
12100 4605
14400 5353
16900 6268
EOD

set table $AVG
    plot $Data u 1:2 smooth unique
unset table

set key top left noautotitle
set offset graph 0.1, graph 0.1, graph 0.1, graph 0.1

plot $Data u 1:2 w p pt 7 lc rgb "red" ti "Data", \
        '' u 1:2 smooth unique w lp pt 7 lw 2 lc rgb "blue" ti "Average", \
      $AVG u 1:2:2 w labels offset 0,2.5
### end of script

Result:

enter image description here

Upvotes: 0

psibar
psibar

Reputation: 2000

If the number of tests is the same for all points, then this can be easily be done with the help of the smooth frequency option:

plot "datafile.txt" u 1:($2/3) smooth frequency with points

Type help smooth frequency in gnuplot to get more information about this option.

Upvotes: 1

Related Questions