1

I'd like to plot a histogram data already created, stored in hist.dat as:

#hist1
100
1
9
10
30
30
10
9
1

Where the (zeroth line is a comment), first line contains the summation of the y value of the histogram, and x values are 1, 2, ... (the line number). So without normation, I could use

plot "hist.dat" every::1 using 0:1

and with normation I could use

plot "hist.dat" every::1 using 0:($1/100)

The question is how can I refer the summated value (100)? Because I don't want to pre-read the file just to create a correct gnuplot code, so I dont't want to write down the value implicit. I already tried

plot "hist.dat" using 0:($1/(columnhead+0))

but columnhead cannot called within using (it is a string, that's why I tried to add 0 to make it int).

I don't want to modify the file or create a new one based on this one, I want to just use the appropriate gnuplot command. I would like to avoid neglecting the summated value and recalculating it again with gnuplot.


Solution: according to andyras who give the correct answer, a bit improved method is

first(x) = ($0 == 0) ? (first = column(x), 1/0) : first

plot "hist.dat" using 0:($1/first(1))

So you can use this to plot histograms if you have multiple columns as if the hist.dat were

#hist1  hist2
10000   8000
1000    50
9000    70
1000    1100
3000    4500
3000    1200
1000    700
9000    380
1000    
DanielTuzes
  • 2,494
  • 24
  • 40

1 Answers1

2

How can I refer the summated value (100)? (without pre-reading the file)

Yes, using a gnuplot function:

first(x) = ($0 == 0) ? (first = $1, 1/0) : first

plot "hist.dat" using 0:($1/first($1))

If it is reading the first line, the function assigns the value from that line to the variable first and returns 1/0 (gnuplot treats it as missing data and won't extend the x range to include that point). Otherwise the function returns the value of first.

This way you don't even have to use every ::1.

If you didn't mind rereading the file you could use the stats command to find out the largest value in the file.

andyras
  • 15,542
  • 6
  • 55
  • 77
  • Believe it or not, I never realized you could use the fields `$1`, `$0`, `$...` inside a gnuplot function. Also, very clever use of an inline function inside the ternary operator as well -- I wouldn't have thought that was possible either. All around, this is a fantastic answer. +1 (and a Merry Christmas) from me. – mgilson Dec 26 '12 at 00:37
  • Thanks, and Merry Christmas yourself! I actually drew some inspiration from one of your answers which taught me that you can comma-separate commands inside a function definition. (http://stackoverflow.com/questions/13655048/display-underscore-rather-than-subscript-in-gnuplot-titles/13655849#13655849) – andyras Dec 26 '12 at 01:29
  • The answer is pretty good but you don't use the information (argument) passed to function `first`. An improved solution for multiple-columned files can be seen in the question part. – DanielTuzes Jan 09 '13 at 10:16