Back to demo index

gnuplot demo script: array.dem

autogenerated by webify.pl on Sat Oct 1 21:59:00 2022
gnuplot version gnuplot 5.4 patchlevel 5
#
# Use an array to hold the sum of values in each columnn
#
array Sum[20]
do for [i=5:13] { 
  stats 'immigration.dat' skip 2 using (column(i)) nooutput
  Sum[i] = STATS_sum
  print sprintf("Sum[%2d] = %d",i,Sum[i])
}
#
# Use the stored sum to normalize values in each column
#
set style fill transparent solid 0.2 border -1
set style data filledcurve y=0
set tics scale 0
set xrange [] noextend
set xlabel "Decade"
set title "Use of an array to aid normalization and to plot summed values"

set multiplot
plot for [i=5:13] 'immigration.dat' using 1:(column(i)/Sum[i]) title columnhead

#
# Plot the array values themselves in a separate inset
#

set size 0.4, 0.3
set origin 0.05, 0.6

unset xtics
unset ytics
set border 0
set xlabel "Total"
set xrange [4:15]
set yrange [0:*]
unset title
set boxwidth 1.0 absolute

plot Sum using 1:2:($1-4) with boxes lc variable notitle

unset multiplot


Click here for minimal script to generate this plot


reset

set title "Fit function to values stored in an array"
set xtics nomirror

array A[100]
do for [i=1:100] { A[i] = sin(2*pi*i/100.) + 0.1*rand(0) }
a = b = c = 0.01
set key title "before fit"
plot A with points title "Array A", c+cos(a+b*x) with lines


Click here for minimal script to generate this plot



set fit nolog
fit c+cos(a+b*x) A using 1:2 via a,b,c

set key title "after fit"
plot A with points title "Array A", c+cos(a+b*x) with lines


Click here for minimal script to generate this plot


reset

#
# Illustrate loading an array from a data file
# This could be done using a preliminary 'plot' command, perhaps together
# with 'set table' if you don't actually want generate a plot, but here
# we use 'stats'. The first run is just to pick up the array size needed.
# The second run stores one value into each array element.
#
set title "Illustrate loading an array from a column in a data file\n".\
          "Note that first data point in the file is 'line 0'\n".\
          "but it goes into array element Data[1]"

set yrange [0:20]
set border 3
set tics nomirror
set key left center nobox

FILE = 'orbital_elements.dat'

stats FILE index 2 using 2 prefix "A"
array Data[A_records]

stats FILE index 2 using (Data[int($0+1)] = $2) prefix "B"

plot FILE index 2 using 2 title "Data from file" with lines lw 2, \
     Data using 0:2:(sprintf("Data[%d]",$1)) with labels point pt 6 ps 2 center offset 0,1 title "Data[i]"


Click here for minimal script to generate this plot


reset