Plotting GNUPlot graph after computation - gnuplot

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.

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.

Dots and empty dots in gnuplot

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.

gnuplot conditional plotting with if

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.

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.

gnuplot removing outliers when plotting a data file

I need to plot a data file of 2 colums using gnuplot, scatter plot is what I need I think. My understanding of gnuplot goes as far as :
plot "first_click" using 2:1
3 lines from head and tail of my data looks as follows:
1 612856
3 3840538
5 5240597
.
.
.
139845 1
141101 1
141584 1
I am expecting my scatter plot to show a logarithmic trend, however my data (as most data) has tons of outliers . So I need to do one of two things:
Automatically "zoom" to where most of the data is.
Automatically prune outliers.
Provide a predicate for each of the columns to manually prune the data, and perhaps predicates that can take both columns of an entry in scope --e.g., !column1 > x && ! column2 == 1
Precision is not a concern.
At this stage I prefer 1 and 2, but I'd like to see if option 3 is possible as well since I am a programmer and not a statistician.
You could also try
plot "first_click" using 2:1 smooth bezier with lines
This has the side effect of not showing most outliers.
gnuplot should automatically zoom to fit the data plotted (if not, you can use reset yrange, xrange to auto-zoom again). If the outliers are pruned prior to plotting then your first requirement would already be met.
Number two and three could be achieved by modifying your plot command as follows:
plot "first_click" using ($2 != 1 ? $2 : 1/0):($1 < x ? $1 : 1/0)
Would plot only values for which the second column is not equal to 1 and the first column is less than x. Where x is the value at which you want to start pruning outliers. 1/0 is a way of telling gnuplot the point is invalid and it won't be plotted.

Resources