How to plot graph with calculating average on abscissa in gnuplot - gnuplot

My data-file is formatted as fallows:
100;2.123;4.123
100;2.113;5.213
100;2.544;6.234
100;2.324;4.234
200;2.543;3.123
200;2.543;5.123
...
First column is a parameter of a function, second and third are the result of a function. The values of 2nd and 3rd are different for the same values of 1st column, because of other factors, and I want to plot a graph that calculates arithmetic average for all values from 2nd and 3rd column that have the same 1st.
Is there any way gnuplot can do this?

To calculate the arithmetic average of all values which share the same 1st value, you can use smooth unique. To get the average of all values of the 2nd and 3rd column for the same abscissa, you can use
set datafile separator ';'
plot 'datafile' using 1:(($2+$3)/2.0) smooth unique
That makes the data monotonic in the x-values and then replaces all points with the same abscissa with one point having the averaged y-value.
If e.g. you want only the averaged values of the 2nd columns, you would instead use
plot 'datafile' using 1:2 smooth unique

yes !
First of all replace each ; by a blank then
try this command
plot "your_data_file" using ($1):(($2+$3)/2)

Related

Gnuplot: calculated X tic labels for data file without X column [duplicate]

I'm plotting a column of data which represents a time series in gnuplot. Every value represents a time value after 500 iterations / time units. Can I tell gnuplot to multiply the x-values it displays by 500?
I thought this would be a standard problem since every time one has to plot a time series one needs to tell the plotting program what time unit each iteration has.
I don't want to create an extra column with x-values manually, since I have a lot of different data of different length. I don't want to create a x column for everyone of them.
If you have only a single column, gnuplot uses the row number as x value. This can be accessed by the pseudo column 0 and scaled like
plot 'datafile' using ($0*500):1
or equivalently, if you're calling this from a shell script
plot 'datafile' using (column(0)*500):1

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

set gnuplot precision for a specific column

I have a data file with 3 columns and I'd like to plot a 2D map plot. What I do in gnuplot is:
p'datafile' u 1:2:3 with image
for some set of data, the data on the 3rd column (say Z) are different with order of 0.01, e.g 1.56, 1.58, 1.59 etc. I'd like to skip those small difference and consider them all 1.5. How can I set gnuplot to consider only up to one digit after decimal for 3rd column? Thanks!
You can use the floor function to round your numbers down:
plot 'datafile' using 1:2:(floor($3*1e1)/1e1) with image
This sets all your numbers to 1 decimal place. If you wanted to do the same thing for a higher number of decimal places, you could change the 1e1 to 1eN, where N is the number of decimal places you want.

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.

Plotting GNUPlot graph after computation

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.

Resources