0

My data file has multiple y values for one x value. I need to plot the max and min y value for all the x values. Is there a command in gnuplot to do so?

Eg: data file

      x    y
      1    10          
      2    6
      1    14
      1    12
      2    25
      2    14

I would like a plot where 1 on x-axis has 10 and 14 plotted and 2 on y-axis has 6 and 25.

I found stats but that finds the min and max values for the entire data set but not for individual x values.

There is Candlesticks option but that is for a different set of data

3
  • What have you got so far? Commented Sep 7, 2015 at 9:41
  • Gnuplot cannot do this. Use an external tool or script to preprocess your data. Commented Sep 7, 2015 at 13:04
  • You mean you want to replace all consecutive lines with same x by a single line that has the maximum y value of those? You might do an ugly hack using the serial evaluation operator, but i could not recommend it. Commented Sep 8, 2015 at 11:38

1 Answer 1

1

Gnuplot was designed to quick plot preprocessed data.
Under Linux, however, you can take advantage of many ready tools to manipulate your data.
If your dataset is not huge you can choose to use some of them inline from gnuplot.

sort can be one of this (as well as awk) here you read 4 times your file.

plot  "<sort data.dat -k2 -n   | sort -n -k1 -u" t "Min" \
     ,"<sort data.dat -k2 -n -r| sort -n -k1 -u" t "Max"

With awk you can do a finer work scanning your file only one time

set xlabel "My x axis [a.u.]"
set ylabel "My y axis [a.u.]"
set style fill transparent solid 0.4

plot "< awk '{ Q[$1]++;                   \
  if (Q[$1]==1){ Min[$1]=$2;Max[$1]=$2; } \
  else                                    \
  {  if (Min[$1]>$2) {Min[$1]=$2;}        \
     else {if (Max[$1]<$2) Max[$1]=$2;} } \
  }                                       \
  END {for (i in Min) print i,Min[i],Max[i]}' data.dat " \
  using ($1):($2):($3) w filledcurve  title "Min and Max" lc rgb "#00A000"

awk plot

Notes:

  • I purged the 1st line of your file for simplicity
  • plot "< command " it executes a command and it uses it as input data
  • man sort and man awk to have the full help of the sort and awk programs
  • check associative arrays in awk to deeper understand awk.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.