0

I have 3 columns and 1000 rows of integers in .dat file and I have to plot it in the graph in the way that first column is on the x-axes and sqrt(c2²+c3²) is on the y-axes, where c2 is from the second column and c3 is from the third column, using gnuplot script.

Normally I use something like plot <somefile.dat> using 1:2 but now I have to use second and third column somehow like that using 1:sqrt(2²+3²).

1
  • 1
    I think you want plot "somefile.dat" using 1:(sqrt($2*$2+$3*$3)) (yes, that will do it) Inside (....) you refer to the column numbers with '$' allowing you to construct your equation. Commented Mar 10, 2020 at 18:08

1 Answer 1

4

To construct equations from column values from your datafile, gnuplot provides a parenthesis grouping, e.g. (your equation here). In order to define your equation within parenthesis, you refer to the column value wanted by prefixing the column number with a '$' (e.g. $2 refers to the value from column 2, $3 refers to the value from column 3, etc..) and you can use those references as many times as needed within the parenthesis and each use will be replaced by the value from the numbered column.

In your case to have the 1st column be your independent x-values and your equation result the dependent value drawing the numbers from columns 2 & 3, you can do:

plot "somefile.dat" using 1:(sqrt($2*$2+$3*$3))

A short example with the input file as:

$ cat somefile.dat
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10

Creating a short plot file for convenience:

$ cat some.plt
plot "somefile.dat" using 1:(sqrt($2*$2+$3*$3))

You can generate your plot with

$ gnuplot -p some.plt

enter image description here

Look things over and let me know if this is what you needed.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you answered my question perfectly!
Glad it helped. gnuplot is an awesome graphing package, but finding complete documentation for it is like pulling-teeth. It can do just about anything you need done -- it just may take a while to figure out how to do it :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.