Given the following data file 'data.dat' composed of three data sets
-2.30368 2.44474
-2.22212 0.0250215
-2.13275 0.312357
-2.10241 0.13895
-2.63484 737.779
-2.44552 0.0156069
-2.1611 0.0360564
-1.98332 0.047829
-2.55816 1.91885
-2.45481 0.0410066
-2.27375 0.0593876
-1.95196 0.0220463
I want to plot all data sets on the same plot, by powering the second column to the index of the data set
pl 'data.dat' u ($1):(($2)**0) i 0, '' u ($1):(($2)**1) i 1, '' u ($1):(($2)**2) i 2
Is there a way to do this automatically for all indexes?
Yes, i think it's possible using a loop structure.
You can try, for instance, the command line
p for [k=0:MAX] 'data.dat' u ($1):($2**k) i k
where k is growing from 0 to your MAX number previously defined in gnuplot. If you want more about loop structure in plotting data with gnuplot you can take a look on this other question on Stack Overflow.
Related
How may I change the plot titles and subtitles when using plot command on linnet object. For example
library(spatstat)
first = runiflpp(10, as.linnet(chicago), nsim = 2)
plot(first)
This code above gives two realisations of a a point process and a plot with the plot command because we requested for nsim=2. But it plots the two realisations with plot title 'simulation 1' and 'simulation 2'.
How can I change the subplot titles for example from simulation 1 to experiment 1?
thank you
The simplest way would be to change the names of the items in the list:
names(first) <- paste("experiment", 1:2)
Alternatively you can change the argument main.panel in plot.solist (see ?plot.solist for all the options):
plot(first, main.panel = paste("experiment", 1:2))
I have a data file containing a gaussian function, and an other date file that contains one column with 3 rows. Those three row are all constant which are
1: mean+variance
2: mean
3: mean-variance
from the gaussian in the first file.
I would like to plot all these as constant lines on the gaussian function. I've tried the "every" command, (plot "stat.dat" every ::0::0 w lines) which didn't work.
Thank you, any help is appreciated.
Do you mean something like this?
set terminal pngcairo
set output "gauss.png"
set samples 1000
x0 = -5
s2 = 1
set xrange [-10:10]
set yrange [0:0.5]
plot (1/sqrt(2*pi*s2))*exp(-(x-x0)**2/(2*s2)) title "Gaussian", \
"stat.dat" u 1:(5) every ::0::0 w impulse title "mean + variance", \
"stat.dat" u 1:(5) every ::1::1 w impulse title "mean", \
"stat.dat" u 1:(5) every ::2::2 w impulse title "mean - variance"
I have replaced your data file which contains the gaussian function by an analytical expression. The result looks as follows:
I compute the iterated positions of multiple particles, so that my output file looks like :
x1(t=0) y1(t=0)
x2(t=0) y2(t=0)
...
xn(t=0) yn(t=0)
x1(t=1) y1(t=1)
...
xn(t=1) yn(t=1)
(a lot of blocks)
x1(t=p) y1(t=p)
...
xn(t=p) yn(t=p)
For example, the particle 1 is on each first line of a block, etc.
I need to plot the trajectory of each particle in a single plot, with points linked with lines. The problem I stumble upon is to link properly the points corresponding to the correct particle. I found some advice recommending to reformat the data but I have no idea how to handle it. It might be also possible to plot directly the trajectories with a plot command but once again I am low on solutions.
You should be able to do it with a loop (in gnuplot >= 4.6) and the index option to the plot command:
p = (number of particles)
plot for [i=0:p] 'data.dat' index i with linespoints
The with linespoints option also sounds like what you want, which links the data points with lines.
Unfortunately, there is no way to do this with your current datafile setup. You can make a plot which doesn't connect the points using the every (e) keyword:
plot for [i=0:NPOINTS-1] 'test.dat' e ::i::i w p
But, that's not very helpful really if you want the datasets connected, you need to "invert" your data. I'd use python because it's super easy:
#pythonscript.py
import sys #allow us to get commandline arguments
#store data as
#[[x1(t=0) y1(t=0),x2(t=0) y2(t=0),x3(t=0) y3(t=0),...],
# [x1(t=1) y1(t=2),x2(t=2) y2(t=2),x3(t=2) y3(t=2),...],
# ...
# [x1(t=N) y1(t=N),x2(t=N) y2(t=N),x3(t=N) y3(t=N),...],
#]
with open(sys.argv[1]) as fin:
data = []
current = []
data.append(current)
for line in fin:
line = line.rstrip()
if line:
current.append(line)
else:
current = []
data.append(current)
#now transpose the data an write it out. `zip(*data)` will give you:
#[(x1(t=0) y1(t=0),x1(t=1) y1(t=1),x1(t=2) y3(t=2),...),
# (x2(t=0) y2(t=0),x2(t=1) y2(t=1),x2(t=2) y2(t=2),...),
# ...
# (xN(t=0) yN(t=0),xN(t=1) yN(t=1),xN(t=2) yN(t=2),...),
#]
for lst in zip(*data):
for dpoint in lst:
print dpoint
print
For me, given the input file (test.dat):
x1(t=0) y1(t=0)
x2(t=0) y2(t=0)
xn(t=0) yn(t=0)
x1(t=1) y1(t=1)
x2(t=1) y2(t=1)
xn(t=1) yn(t=1)
x1(t=p) y1(t=p)
x2(t=p) y2(t=p)
xn(t=p) yn(t=p)
running python pythonscript.test.dat gives:
x1(t=0) y1(t=0)
x1(t=1) y1(t=1)
x1(t=p) y1(t=p)
x2(t=0) y2(t=0)
x2(t=1) y2(t=1)
x2(t=p) y2(t=p)
xn(t=0) yn(t=0)
xn(t=1) yn(t=1)
xn(t=p) yn(t=p)
Now you can plot that using the solution by andyras:
plot for [i=0:NP] '< python pythonscript.py data.dat' index i w lp
I want to plot data using fit function : function f(x) = a+b*x**2. After ploting i have this result:
correlation matrix of the fit parameters:
m n
m 1.000
n -0.935 1.000
My question is : how can i found a correlation coefficient on gnuplot ?
You can use the stats command in gnuplot, which has syntax similar to the plot command:
stats "file.dat" using 2:(f($2)) name "A"
The correlation coefficient will be stored in the A_correlation variable. (With no name specification, it would be STATS_correlation.) You can use it subsequently to plot your data or just print on the screen using the set label command:
set label 1 sprintf("r = %4.2f",A_correlation) at graph 0.1, graph 0.85
You can find more about the stats command in gnuplot documentation.
Although there is no direct solution to this problem, a workaround is possible. I'll illustrate it using python/numpy. First, the part of the gnuplot script that generates the fit and connects with a python script:
file = "my_data.tsv"
f(x)=a+b*(x)
fit f(x) file using 2:3 via a,b
r = system(sprintf("python correlation.py %s",file))
ti = sprintf("y = %.2f + %.2fx (r = %s)", a, b, r)
plot \
file using 2:3 notitle,\
f(x) title ti
This runs correlation.py to retrieve the correlation 'r' in string format. It uses 'r' to generate a title for the fit line. Then, correlation.py:
from numpy import genfromtxt
from numpy import corrcoef
import sys
data = genfromtxt(sys.argv[1], delimiter='\t')
r = corrcoef(data[1:,1],data[1:,2])[0,1]
print("%.3f" % r).lstrip('0')
Here, the first row is assumed to be a header row. Furthermore, the columns to calculate the correlation for are now hardcoded to nr. 1 and 2. Of course, both settings can be changed and turned into arguments as well.
The resulting title of the fit line is (for a personal example):
y = 2.15 + 1.58x (r = .592)
Since you are probably using fit function you can first refer to this link to arrive at R2 values.
The link uses certain existing variables like FIT_WSSR, FIT_NDF to calculate R2 value.
The code for R2 is stated as:
SST = FIT_WSSR/(FIT_NDF+1)
SSE=FIT_WSSR/(FIT_NDF)
SSR=SST-SSE
R2=SSR/SST
The next step would be to show the R^2 values on the graph. Which can be achieved using the code :
set label 1 sprintf("r = %f",R2) at graph 0.7, graph 0.7
If you're looking for a way to calculate the correlation coefficient as defined on this page, you are out of luck using gnuplot as explained in this Google Groups thread.
There are lots of other tools for calculating correlation coefficients, e.g. numpy.
I am new to gnuplot, I need to plot my data and display a small circle or an arrow at each end of the line chart. how can I do that?
I use this command to display the line chart:
plot 'data.txt' with lines
I don't know if there is a way to make lines have something at the end automatically, but I found a workaround. With this data file:
1 1
2 3
3 2
and the following script:
set term png
set out 'plot.png'
stats 'data.dat' name 'a'
# plot line, then circle only if it is the last data point
plot 'data.dat' t 'data', \
'' u ($0==(a_records-1)?$1:1/0):2 with points pt 7 ps 2 t 'end'
I can make a plot like this:
The stats command is to find the number of data points, then the dual plot command draws the line connecting the data points, then a circle only on the last data point (determined with the a_records variable. An arrow would be trickier to draw...
To find more info about different point/line style options, the test command at the gnuplot command line is your friend.