DanielTuzes
DanielTuzes

Reputation: 2744

how to manipulate data with gnuplot's plot with a number stored in the same file?

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    

Upvotes: 1

Views: 5965

Answers (1)

andyras
andyras

Reputation: 15910

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.

Upvotes: 2

Related Questions