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.
Related
I need to draw dots and empty dots in a plot to show the fixed points. They should look like in the figure below: an empty dot (a circle) and a thick dot.
Is there any way to plot something like that?
Thanks
(source: emathematics.net)
If you wish to use the built in point types, there are usually (depending on the terminal) a filled circle and an empty circle. Issuing the test command will show you what is available. For instance, with the wxt terminal, I see
which shows me that I can get a filled circle with point type 7 and an empty one with point type 6 (we don't worry about the different colors as those are inherited from the line type).
Now, the easy way to get our desired plot is to just issue plot datafile with points pt 6 or point type 7. It is trickier if you need it to depend on your data.
Suppose that my data looks like this:
1 3 0
5 8 1
2 6 0
3 2 1
The first two columns are the x and y coordinates and the third tells me if I should use an empty dot (0) or a filled dot (1). Unfortunately, gnuplot does not support a variable point type command (something like plot datafile u 1:2:($3+6) with linespoints pt var), which is exactly what we need here.
In order to plot our data, we will make two passes. The first will plot the lines and the empty circles (for all points), and the second will plot the filled circles (for only the points that should be filled - overwriting the empty circles).
plot datafile u 1:2 with linespoints pt 6, \
datafile u 1:($3==1?$2:1/0) with points pt 7 lt 1
This command will plot the lines and the empty circles first. Next it plots the filled circles only when they are needed. To do this, it computes a conditional y-coordinate. If the third column is 1, it uses the second column as the y-coordinate. If not, it uses the invalid value 1/0, which causes gnuplot to skip the point. In order to keep the colors and everything else the same, we use lt 1 (which was used by default in the first plot command segment).
Note: I have put in a feature request for a pointtype variable option, and it looks like it may work its way into a future version. A preliminary patch is available implementing this under feature request 437.
Updated Note: The pointtype variable option is currently availabe in the development version of gnuplot.
I have a data file with two columns
10 0.5
20 0.8
25 0.3
15 0.6
I want to plot the second column if the first column is less than or equal 20. Problem is, I want to skip the rows where the first column is greater than 20, however gnuplot forces me to do something in the conditional part.
The command is
plot 'data.txt' u ($1<=20?$2:0) with points
As you can see, I have to specify to put a point of ZERO. I don't want that! I want to skip....
To skip a point in gnuplot you must give it an invalid value like 1/0:
plot 'data.txt' u 1:($1 <= 20 ? $2 : 1/0) with points
For some plotting styles the presence of invalid values deserves some attention. If the remaining points should be connected e.g. with lines, the line is interrupted at an invalid point.
Since gnuplot version 5.0.6 one can use set datafile missing NaN to treat invalid points like missing ones. The filtered data then behaves as if the invalid points don't exist. See https://stackoverflow.com/a/46070360/2604213 for a working example.
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.)
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.
I have a data file that lists hits and misses for a certain cache system. Following is the data file format
time hits misses
1 12 2
2 34 8
3 67 13
...
To plot a 2D graph in GNUPlot for time vs hits, the command would be:
plot "data.dat" using 1:2 using lines
Now I want to plot a graph of time vs hit-ratio, For this can I do some computation for the second column like :
plot "data.dat" using 1:2/ (2 + 3) using lines
Here 1, 2, 3 represent the column number.
Any reference to these kind of graph plotting will also be appreciated.
Thanks in advance.
What you have is almost correct. You need to use $ symbols to indicate the column in the calculation:
plot "data.dat" using 1:($2/($2 + $3))
Since you are using $n to refer to the column numbers, you now are able to use n to refer to the number itself. For example,
plot "data.dat" using 1:(2 * $2)
will double the value in the second column.
In general, you can even plot C functions like log and cos of a given column. For example:
plot "data.dat" u 1:(exp($2))
Note the parens on the outside of the argument that uses the value of a particular column.
See here for more info.