gnuplot how to merge two every commands in one line - gnuplot

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.)

Related

How to divide Y-axis data [duplicate]

I have some measured data, experiment.dat which goes like this:
1 2
2 3
Now I want to plot them via some command line
plot "experiment.dat" using 1:2 title "experiment" with lines lw 3
Is there some way how to scale the different lines with some scaling factor like -1?
Yes, you can do any kind of calculations inside the using statement. To scale the y-value (the second column) with -1, use
plot "experiment.dat" using 1:(-1*$2)
You don't need to multiply the column by minus one, you can simply use:
p "experiment.dat" u 1:(-$2)
at least with Version 5.4 works fine.
You can also only use the initial letter of every command.

Plotting n lines each second with Gnuplot

I have to say that i am new to Gnuplot.
I have a file with 3 columns of datas and I managed plotting it with
gnuplot "name.txt" obtaining a 3D graph. Now i would want to graph n lines of that txt each second. I read that it is possible if the file update itself each second but it is not my case.
I have this file with datas that are calculated by a c++ program and represent the position of an object every second . Is it possible to live update the Graph so that you can see n points plotted each second ?
Until now I managed to do this byte Command : splot "data.txt" every ::1::10 . With this Gnuplot draw n lines form data.txt
Here is one possibility:
do for [N=10:*:10] {
splot 'name.txt' every 1::1::N using 1:2:3 with points
pause 1.0
}

Auto plotting all columns

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?.

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.

Scale measurement data

I have some measured data, experiment.dat which goes like this:
1 2
2 3
Now I want to plot them via some command line
plot "experiment.dat" using 1:2 title "experiment" with lines lw 3
Is there some way how to scale the different lines with some scaling factor like -1?
Yes, you can do any kind of calculations inside the using statement. To scale the y-value (the second column) with -1, use
plot "experiment.dat" using 1:(-1*$2)
You don't need to multiply the column by minus one, you can simply use:
p "experiment.dat" u 1:(-$2)
at least with Version 5.4 works fine.
You can also only use the initial letter of every command.

Resources