Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gnuplot: Plot data and use max of repeated x value

Tags:

plot

gnuplot

I want to plot a curve of a huge data file with two columns:

datafile (example):

#dateYMD sum
2014-02-01 70
2014-02-01 85
2014-02-01 95
2014-02-02 116
2014-02-02 123
2014-02-09 130
2014-02-09 134
2014-02-11 145

if I use "plot 'data.txt' using 0:1 with lines" and set the date format etc right I'll get a plot which is nearly correct, but unfortunately it uses the "lower/upper" value if more than one line per date is given. I want to plot the maximum of the sum-column per day in my plot. I want to use the last/highest given value for each day.

like image 706
JonnyZoo Avatar asked Feb 24 '14 12:02

JonnyZoo


Video Answer


2 Answers

Another solution might be the code below. It is assumed that the dates are strictly ascending or descending, but not random. The accepted solution is certainly the more efficient solution but not so easy to follow. The basic idea is to write the last date (of several identical dates) and its value into a new datablock and simply plot this new dataset.

### start code
reset session

$Data <<EOD
#dateYMD sum
2014-02-01 70
2014-02-01 85
2014-02-01 95
2014-02-02 116
2014-02-02 123
2014-02-09 130
2014-02-09 134
2014-02-11 145
EOD

stats $Data nooutput
set table $Dummy
set print $Data2
tmp = ""
do for [i=STATS_records-1:0:-1] {
   plot $Data u (a=stringcolumn(1),b=stringcolumn(2),$2) every ::i::i with table
   if (tmp ne a) { print sprintf("%s\t%s",a,b); tmp = a}
}
set print
unset table

set xdata time
set timefmt "%Y-%m-%d"
set yrange[0:160]
plot $Data2 u (timecolumn(1)):2 w lp lw 2 pt 7 t "maximum values"
### end code
like image 61
theozh Avatar answered Sep 20 '22 23:09

theozh


That is a bit tricky. My answer to Plotting different columns on the same file using boxes shows how that can be done for a numeric x-axis. If you have time data, you must use timecolumn(1) instead of $1, and thats it:

reset
xval = -1e10
max(x, y) = (x > y ? x : y)
maxval = 0

set timefmt '%Y-%m-%d'
set xdata time

plot 'data.txt' using (val = $2, timecolumn(1)):\
     (maxval_prev = (xval == timecolumn(1) ? maxval : 0), \
      maxval = (xval == timecolumn(1) ? max(maxval, val) : val),\
      xval = timecolumn(1), \
      (maxval > maxval_prev ? maxval-maxval_prev : 0)\
     ) \
     smooth frequency lw 3 with linespoints t 'maximum values'

That gives the result (with 4.6.3):

enter image description here

See the above linked answer for a more detailed explanation.

like image 28
Christoph Avatar answered Sep 17 '22 23:09

Christoph