Back to demo index

gnuplot demo script: running_avg.dem

autogenerated by webify.pl on Fri Oct 28 21:05:34 2016
gnuplot version gnuplot 5.0 patchlevel 5
#
# This script demonstrates the use of assignment operators and
# sequential expression evaluation to track data points as they
# are read in.
#
# We use the '=' and ',' operators to track the running total
# and previous 5 values of a stream of input data points.
#
# Ethan A Merritt - August 2007
#
# Define a function to calculate average over previous 5 points
#
set title \
    "Demonstrate use of assignment and serial evaluation operators\n" \
    . "to accumulate statistics as successive data lines are read in\n"
set key invert box center right reverse Left
set xtics nomirror
set ytics nomirror
set border 3

samples(x) = $0 > 4 ? 5 : ($0+1)
avg5(x) = (shift5(x), (back1+back2+back3+back4+back5)/samples($0))
shift5(x) = (back5 = back4, back4 = back3, back3 = back2, back2 = back1, back1 = x)

#
# Initialize a running sum
#
init(x) = (back1 = back2 = back3 = back4 = back5 = sum = 0)

#
# Plot data, running average and cumulative average
#

datafile = 'silver.dat'
set xrange [0:57]

set style data linespoints

plot sum = init(0), \
     datafile using 0:2 title 'data' lw 2 lc rgb 'forest-green', \
     '' using 0:(avg5($2)) title "running mean over previous 5 points" pt 7 ps 0.5 lw 1 lc rgb "blue", \
     '' using 0:(sum = sum + $2, sum/($0+1)) title "cumulative mean" pt 1 lw 1 lc rgb "dark-red"


Click here for minimal script to generate this plot