11

The following gnuplot code works good:

plot 'data.txt'  using 2:4  '   %lf %lf %lf %lf' title "1 PE" with linespoints;

In the following code, I want to say: "Use the number from column 4, but then divide it by the number from column 3". Or: "Use the number from column 2, but divide it by the constant 2.0". The following code demonstrates what I try to achieve, but it does not work.

plot 'data.txt'  using 2:4/4  '   %lf %lf %lf %lf' title "1 PE" with linespoints;
plot 'data.txt'  using 2:4/2.0  '   %lf %lf %lf %lf' title "1 PE" with linespoints;

Is something like this possible?

2 Answers 2

26

I don't usually work with datafiles formatted like this, but I think you're looking for something like:

#divide column 4 by column 3
plot 'data.txt'  using 2:($4/$3)  '   %lf %lf %lf %lf' title "1 PE" with linespoints

#divide column 4 by constant 10.0
plot 'data.txt'  using 2:($4/10.0)  '   %lf %lf %lf %lf' title "1 PE" with linespoints

As a side note, I don't think there's any reason for passing the format portion to using here. Gnuplot splits the datafile on whitespace just fine:

plot 'data.txt'  using 2:($4/$3) title "1 PE" with linespoints

Should work just fine.

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

3 Comments

Many thanks, this works. Also, yeah, the format strings were not necessary, cool!
What about dividing a column from file1 by a column from file 2?
@Ruzayqat -- I haven't worked in Gnuplot for quite some time, but back in the day, there was no way to do that. You'd need some sort of tooling to zip the columns together and then you could pipe the data in.
14

Just to add to the given answer, if you prefer to use column names (when your data has headers) then the following syntax can be used:

plot 'data.dat' using 'header1':(column('header2')*column('header3')) with lines

Edit

I have received some downvotes for my answer, but I have no clue why. While I could easily find how to do calculations with numeric column references, the trick with named columns was almost impossible to find in the documentation or on SO, so I still hope my answer can help others.

2 Comments

This is actually a very helpful answer.
Thx for the column keyword. That helped me to use a variable instead of $1., e.g. column(my_column_number)

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.