Neverland
Neverland

Reputation: 775

Is it possible to let gnuplot calculate the average value from a column?

I want to create with Gnuplot a diagram from the columns 4 and 10 of my input file.

The input data file looks this way (the ^^^^^ symbols only highlight the relevant columns).

                           ^^^^^                               ^^^^^
2017-05-29 15:41:29 10 512 0.215 4.332 0.430 1.451 1.158 0.199 7.785
2017-05-29 15:41:44 10 512 0.201 1.206 0.304 1.186 1.068 0.196 4.161
2017-05-29 15:42:00 10 512 0.195 1.223 0.301 1.206 1.055 0.204 4.184
2017-05-29 15:42:15 10 512 0.191 1.234 0.302 1.157 1.102 0.191 4.177
2017-05-29 15:42:30 10 512 0.196 1.233 0.297 1.246 1.129 0.206 4.307
...

I want two bars. One shows the average value of columns 4 and the other one the avarage value of columns 10.

Is it possible to let gnuplot calculate the average value of a column?

Upvotes: 5

Views: 13661

Answers (1)

Christoph
Christoph

Reputation: 48390

1. Plot all datapoints and visualize the mean

Use stats to calculate statistical values for your data set:

stats 'file.dat' using 5:10 nooutput

The mean values are available as variables STATS_mean_x and STATS_mean_y. These calculations must be done before setting the timefmt and xdata time, because stats doesn't work in time mode.

Note also, that the first column you marked is the fifth column, because date and time are counted as two columns for gnuplot.

reset
$data <<EOD
2017-05-29 15:41:29 10 512 0.215 4.332 0.430 1.451 1.158 0.199 7.785
2017-05-29 15:41:44 10 512 0.201 1.206 0.304 1.186 1.068 0.196 4.161
2017-05-29 15:42:00 10 512 0.195 1.223 0.301 1.206 1.055 0.204 4.184
2017-05-29 15:42:15 10 512 0.191 1.234 0.302 1.157 1.102 0.191 4.177
2017-05-29 15:42:30 10 512 0.196 1.233 0.297 1.246 1.129 0.206 4.307
EOD

stats $data using 5:10 nooutput

set timefmt "%Y-%m-%d %H:%M:%S"
set xdata time
set autoscale xfix

plot $data u 1:5 w lp lt 1 t 'Col 5', '' u 1:(STATS_mean_x) w l lt 1 lw 2 t 'Avg col 5',\
    '' u 1:10 w lp lt 2 t 'Col 10', '' u 1:(STATS_mean_y) w l lt 2 lw 2 t 'Avg col 10'

enter image description here

2. Visualize statistical information about the columns

In order to show meaningful statistical information (not only the mean value) about your data, you can use the boxplot plotting style:

set style data boxplot
plot $data u (1):5:xtic('Col 5') title 'Col 5', '' u (2):10 title 'Col 10'

enter image description here

3. Plotting only boxes with the mean value

You can save the values computed with stats to a data block with set print and plot that one:

stats $data using 5:10 nooutput
set print $mean
print STATS_mean_x
print STATS_mean_y

set style data boxes
set boxwidth 0.7
set style fill solid noborder
set yrange [0:*]
plot $mean using 0:1

enter image description here

Upvotes: 8

Related Questions