Auto plotting all columns - gnuplot

I have a file with several columns of data (the number of columns N might me quite large). I want to plot all the columns as a function of the first one (that is, plot 'Data.txt' using 1:2, 'Data.txt' using 1:3, ..., 'Data.txt' using 1:N). The thing is, I want this command to work when I don't know the number of columns. Is that possible?

You can count the number of columns in your file using awk and then do a looped plot. There might be a function to get the number of columns in your data file already implemented in gnuplot but I do not know it. You can try this:
N=`awk 'NR==1 {print NF}' Data.txt`
plot for [i=2:N] "Data.txt" u 1:i
If your first row contains a comment (starting by #) change NR== to the appropriate value. If you have a variable number of columns for different rows then you might want to complicate the awk command.

#Paul shows a correct answer, but an even simpler variant is possible. You can use an open-ended iteration that stops when it runs out of columns:
plot for [n=1:*] "data.dat" using 1:n title sprintf("Column %d",n)

Seeing that this questions is very old, I still think it is worth revisiting, as you now (Version 5.2) have access to the number of columns in a file without relying on external tools.
DATA = 'path/to/datafile.txt'
stats DATA
will (among other stuff) store the number of columns in the variable STATS_columns, so now you can do something like:
N=STATS_columns
plot for [i=2:N] DATA using 1:i title DATA.' '.i with lines
which will plot all the columns (assuming the first column is used for the x-axis) with legend entries matching the filename plus the column number.
PS: Not sure when this feature was introduced, but it's there now. :)

You will need two script files:
==== main.plt ====
set <whatever>
N=1
load "loop.plt"
==== loop.plt ====
replot "data.dat" u 0:(column(N))
N+=N+1
if(N<4) reread
Function reread cause that the next line to read by gp will be loop.plt:1. Now you will plot first three columns of data.dat. Function replot adds plot to current image.
Or see: how to convert integer to string in gnuplot?.

Related

Performing calculations between multiple data files in gnuplot [duplicate]

I have 2 dat files:
a.dat
#Xs
100 25
200 56
300 75
400 67
b.dat
#Xs
100 65
200 89
300 102
400 167
I want to draw a graph in the gnuplot where the yy values are a ratio between the values of a.dat and b.dat respectively. e.g., 25/65, 56/89, 75/102, and 67/167.
How I do this? I only know make a plot like this, and not with the ratio.
plot "a.dat" using 1:2 with linespoints notitle
"b.dat" using 1:2 with linespoints notitle
You cannot combine the data from two different files in a single using statement. You must combine the two files with an external tool.
The easiest way is to use paste:
plot '< paste a.dat b.dat' using 1:($2/$4) with linespoints
For a platform-independent solution you could use e.g. the following python script, which in this case does the same:
"""paste.py: merge lines of two files."""
import sys
if (len(sys.argv) < 3):
raise RuntimeError('Need two files')
with open(sys.argv[1]) as f1:
with open(sys.argv[2]) as f2:
for line in zip(f1, f2):
print line[0].strip()+' '+line[1],
And then call
plot '< python paste.py a.dat b.dat' using 1:($2/$4) w lp
(see also Gnuplot: plotting the maximum of two files)
The short answer is that... you cannot. Gnuplot processes one file at a time.
The work-around is to use an external tool, e.g. using the shell if you have a unix-like, or gnuplot.
join file1 file2 > merged_file will allow you to merge your files quite easily if the first column is identical in both files. Options allow to join on other columns and manage data missing in either file.
In case there is no common column but hte line number is relevant, paste will do.
In case interpolation is required, because the grids in the two files differ, you have to code this. I have a command-line utility that I distribute as free open-source software and can help: datamerge.
There is a trick if the two datasets don't fit (different sampling in x), but you have a good mathematical model for at least one of them:
fit f2(x) data2 us 1:2 via ...
set table $corr
plot data1 using 2:(f2($1))
unset table
plot $corr using 1:2
This is of course nonsense if both datasets have the same set of independent variables, because you can simply combine them (see other answers).
Just for fun, there is a gnuplot-only and platform-independent solution.
For sure, the command plot '< paste a.dat b.dat' using 1:($2/$4)' from Christoph's answer is unbeatable short (and certainly efficient) in case you are working under Linux or installed the CoreUtils from GnuWin under Windows.
The solution below takes the detour via two long strings. This should work fine unless there is a length limit of strings in gnuplot. I tested only until 100'000 data lines, which will take quite a few minutes. The assumption is that the two files have equal lines with identical x-values. For gnuplot>=5.0 you could write into a datablock instead of a file on disk and do further optimizations.
Script: (works with gnuplot>=4.6.0, March 2012)
### get ratio of numbers from different files
reset
FILE1 = "SO20069641a.dat"
FILE2 = "SO20069641b.dat"
FILE = "SO20069641.dat"
# create some random test data
set samples 100
set table FILE1
plot '+' u (int($0+1)*100):(int(rand(0)*100)+1)
set table FILE2
plot '+' u (int($0+1)*100):(int(rand(0)*100)+1)
unset table
a = ''
b = ''
stats FILE1 u (a=a.' '.strcol(1).' '.strcol(2)) nooutput
stats FILE2 u (b=b.' '.strcol(1).' '.strcol(2)) nooutput
set table "SO20069641.dat"
set samples words(a)/2
splot '+' u (n=2*(int($0+1)),real(word(a,n-1))):(real(word(a,n))):(real(word(b,n)))
unset table
plot FILE u 1:($2/$3) w impulses
### end of script
Result:

How to plot a 2 dimensional function using data stored in a file with gnuplot

Is there any way to compute a 2 dimensional function using data from a file in gnuplot. Suppose I have a function f(x,y) which exist and I want to calculate the new values with data stored in file data.dat
i.e something like
plot f(x,y) using 'data.dat'$1:'data.dat'$2
The command plot is used for plotting one variable against another. If you want to plot a third value against two others (and obtain something that looks 3D), you'll need the splot command. In this case, the command would look like
splot 'mydata.txt' using 1:2:(f($1,$2))
The using keyword specifies what you want to plot based on the contents of a file. The 1 and 2 means that the x and y coordinates will just be the first and second column in the file. For the third coordinate we want the f(x,y) function to be used with the values from the first and second column filled in ($1 and $2).
In case we're doing something more complex than just using a column unmodified, we have to use brackets and a $-sign for the variables. So we also could have written
splot 'mydata.txt' using ($1):($2):(f($1,$2))
as the command. See the gnuplot manual for more information.

Get ratio from 2 files in gnuplot

I have 2 dat files:
a.dat
#Xs
100 25
200 56
300 75
400 67
b.dat
#Xs
100 65
200 89
300 102
400 167
I want to draw a graph in the gnuplot where the yy values are a ratio between the values of a.dat and b.dat respectively. e.g., 25/65, 56/89, 75/102, and 67/167.
How I do this? I only know make a plot like this, and not with the ratio.
plot "a.dat" using 1:2 with linespoints notitle
"b.dat" using 1:2 with linespoints notitle
You cannot combine the data from two different files in a single using statement. You must combine the two files with an external tool.
The easiest way is to use paste:
plot '< paste a.dat b.dat' using 1:($2/$4) with linespoints
For a platform-independent solution you could use e.g. the following python script, which in this case does the same:
"""paste.py: merge lines of two files."""
import sys
if (len(sys.argv) < 3):
raise RuntimeError('Need two files')
with open(sys.argv[1]) as f1:
with open(sys.argv[2]) as f2:
for line in zip(f1, f2):
print line[0].strip()+' '+line[1],
And then call
plot '< python paste.py a.dat b.dat' using 1:($2/$4) w lp
(see also Gnuplot: plotting the maximum of two files)
The short answer is that... you cannot. Gnuplot processes one file at a time.
The work-around is to use an external tool, e.g. using the shell if you have a unix-like, or gnuplot.
join file1 file2 > merged_file will allow you to merge your files quite easily if the first column is identical in both files. Options allow to join on other columns and manage data missing in either file.
In case there is no common column but hte line number is relevant, paste will do.
In case interpolation is required, because the grids in the two files differ, you have to code this. I have a command-line utility that I distribute as free open-source software and can help: datamerge.
There is a trick if the two datasets don't fit (different sampling in x), but you have a good mathematical model for at least one of them:
fit f2(x) data2 us 1:2 via ...
set table $corr
plot data1 using 2:(f2($1))
unset table
plot $corr using 1:2
This is of course nonsense if both datasets have the same set of independent variables, because you can simply combine them (see other answers).
Just for fun, there is a gnuplot-only and platform-independent solution.
For sure, the command plot '< paste a.dat b.dat' using 1:($2/$4)' from Christoph's answer is unbeatable short (and certainly efficient) in case you are working under Linux or installed the CoreUtils from GnuWin under Windows.
The solution below takes the detour via two long strings. This should work fine unless there is a length limit of strings in gnuplot. I tested only until 100'000 data lines, which will take quite a few minutes. The assumption is that the two files have equal lines with identical x-values. For gnuplot>=5.0 you could write into a datablock instead of a file on disk and do further optimizations.
Script: (works with gnuplot>=4.6.0, March 2012)
### get ratio of numbers from different files
reset
FILE1 = "SO20069641a.dat"
FILE2 = "SO20069641b.dat"
FILE = "SO20069641.dat"
# create some random test data
set samples 100
set table FILE1
plot '+' u (int($0+1)*100):(int(rand(0)*100)+1)
set table FILE2
plot '+' u (int($0+1)*100):(int(rand(0)*100)+1)
unset table
a = ''
b = ''
stats FILE1 u (a=a.' '.strcol(1).' '.strcol(2)) nooutput
stats FILE2 u (b=b.' '.strcol(1).' '.strcol(2)) nooutput
set table "SO20069641.dat"
set samples words(a)/2
splot '+' u (n=2*(int($0+1)),real(word(a,n-1))):(real(word(a,n))):(real(word(b,n)))
unset table
plot FILE u 1:($2/$3) w impulses
### end of script
Result:

gnuplot how to merge two every commands in one line

I have a file with 1200 rows. I am trying to use every command which will plot any chunk of data (for example from 6th to 800th data but every 5 points. I know how to exploit every to select first 1000 data (but not any chunk) and every 5 points separately. Is there any way to do that in an one liner?
Plot "file.dat" every ::::1000 every 5 u 1:4 fails to do that. Thanks!
See help every for an explanation of the empty sites in your every ::::1000 command:
Syntax:
plot 'file' every {<point_incr>}
{:{<block_incr>}
{:{<start_point>}
{:{<start_block>}
{:{<end_point>}
{:<end_block>}}}}}
In your case you need only the point parameters, block marks distinct parts of a data file which are separated by one newline.
So you plot command to select every 5th point between the 6th and 800th row is:
plot 'datafile.dat' every 5::6::800 using 1:4
If what you want is to plot every fifth point from only the first 1000 points, I do not think there is a (reasonably simple) way to do that in pure gnuplot. One option is to use an external command to do simple processing on your data file:
plot '< head -n 1000 datafile.dat' every 5
(For details on what that syntax does, type help plot special in gnuplot, or search for 'popen' in the gnuplot docs.)

How to treat the first line of the data file as column labels in gnuplot?

I have a table like this:
A B C D E F G H I
10 23998 16755 27656 17659 19708 20328 19377 18925
20 37298 33368 53936 41421 44548 40756 40985 37294
I use this command to plot
plot "C:/file.txt" using 1:2 with lines smooth bezier, "C:/file.txt" using 1:3 with lines smooth bezier, ...
However, all the labels come out as the file name. Is it possible for gnuplot to read the first row and label the lines accordingly?
set key autotitle columnhead
plot for [n=2:12] 'vv.csv' u 1:(column(n)) w lines title columnhead(n)
n starts from 2 to skip the header.
I checked the documentation and I don't see a way to do it automatically, but you can manually set a title with
plot "file.txt" using 1:2 title "A" with lines smooth bezier ...
I once wrote a script to plot FM radio station frequencies along an axis from 87MHz to 108MHz, using the names of each radio station as vertical labels. This was not a pure gnuplot solution, the input file is processed with perl with make, but I suggest you have a look at it and see if you can use something like that.
You could also use a gnuplot toolkit such as this one for Python if you want have a lot of data to plot and you want to automate the extraction of the titles.

Resources