I have some data in a file, with x in [2,4].
I put some of them, those for x in [2.5, 3.5], in a new file, and then I fit just the second file.
Then, I plot the first file, with all the data, and replot the fit function.
In this way, the fit function is plotted for x in [2,4] but is horrible because it does not fit in [2, 2.5] and [3, 3.5].
So I'd like to have the plot of this fit function only in the right range, but gnuplot doesn't allow me to set a particular x range when using replot.
So, how can I have all the data but the fit function only in the right region in an unique plot?
Put the fit function in a datafile, then plot this datafile together with your original data.
# This is the complete xrange
set xrange [-2:2]
# This creates some test data to play with
set table "data.txt"
plot sin(x)
unset table
# Fit some of the created data points
f(x) = a*x + b
fit [-0.5:0.5] f(x) "data.txt" via a, b
# Plot the fit function to a temporary file
# Note, only the xrange of the fit is used
set table "fit.dat"
plot [-0.5:0.5] f(x)
unset table
# Plot both datafiles in one diagram
plot "data.txt" w l, "fit.dat" w l lw 4
Related
I would like to plot a function f: R -> R^2 like f(t) = (cos(t), sin(t)), but I don't see how to do it.
f(t) = (cos(t), sin(t))
plot f(x) # doesn't work
splot f(x) # doesn't work either and is probably not what I want
Is there a way to plot such a function in gnuplot?
Please check help parametric, help special-filenames, help sampling and this: http://gnuplot.sourceforge.net/demo_5.4/param.html.
Here are two approaches:
Script:
### parametric curves
reset session
fx(t) = cos(t)
fy(t) = sin(t)
set size ratio -1
set xrange[-1.1:1.1]
set yrange[-1.1:1.1]
set multiplot layout 1,2
set parametric
plot [t=0:2*pi] fx(t), fy(t) w l lc "red"
unset parametric
plot sample [t=0:2*pi] '+' u (fx(t)):(fy(t)) w l lc "blue"
unset multiplot
### end of script
Result:
So here is what I'm trying to do.
The values on x axis are from 10000, 20000, 30000, ... 100000. I'm trying to write it like this: 10, 20, 30, 40, ... 100 (only x axis)
Is there some way to do this in Gnuplot?
I have this so far:
(data.dat - example of data)
# x y
10000 +1.24241522E-04
11000 +1.28623514E-04
12000 +1.35229020E-04
13000 +1.43767741E-04
14000 +1.53409148E-04
15000 +1.63788695E-04
16000 +1.75429485E-04
17000 +1.88827813E-04
18000 +2.02984785E-04
19000 +2.20830420E-04
...
(my gnuplot script)
set term png
set out 'example.png'
U0 = 0.00732 #parameters for this particular problem
v1 = 68000
b1 = 6550
v2 = 59600
b2 = 6050
I = sqrt(-1)
A(w, w0, b) = ((w0)**2)/(((w0)**2) - ((w)**2) + 2*I*w*b)
f(x) = U0*abs(A(2*pi*x, 2*pi*v1, b1) - A(2*pi*x, 2*pi*v2, b2))
set xlabel "x"
set ylabel "y"
fit f(x) 'data.dat' u 1:2 via U0, v1, b1, v2, b2
plot 'data.dat' u 1:2 t "Title1" w p, U(x) t "Title2"
set out
But how do I do this?
I've tried this example
How to scale the axes in Gnuplot
but it doesn't work.
See below.
# I modified the things a little bit
f(x) = (.... ... ....)/1000
fit f(x) 'data.dat' u ($1/1000.):2 via U0, v1, b1, v2, b2
plot 'data.dat' u ($1/1000.):2 t "Title1" w p, f(x) t "Title2"
But now the fitted function disappears!
How can I modify x-axis without other function disappearing?
Does there exist a line command in gnuplot for this? I'm sure there has to be a more elegant way of writing this insted of dividing each function by a desired factor.
Two possible ways come to my mind:
if you want to avoid too many zeros in the xtic labels, simply set the xtic label format to engineering
set format x "%.0s%c"
This will show, e.g. 10000 and 100000 as 10k and 100k, respectively.
if you scale (in your case: divide) the x values of the data by factor of 1000, gnuplot will take this x range for plotting the function f(x). Since this is will give x values which are a factor of 1000 too small you have to scale your x values by a factor of 1000 accordingly (in your case: multiply).
Code:
### avoid too many zeros in xtic labels
reset session
# create some random test data
set print $Data
A = rand(0)*10+5
B = rand(0)*50000+25000
C = rand(0)*5000+5000
do for [i=10000:100000:500] {
print sprintf("%g %g",i,A*exp(-((real(i)-B)/C)**2))
}
set print
a=1; b=50000; c=5000 # give some reasonable starting values
f(x) = a*exp(-((x-b)/c)**2)
set fit quiet nolog
fit f(x) $Data u 1:2 via a,b,c
set multiplot layout 1,2
set format x "%.0s%c" # set xtics to engineering
plot $Data u 1:2 w p, \
f(x) w l lc "red"
set format x "%g" # set xtics to default
plot $Data u ($1/1000):2 w p, \
f(x*1000) w l lc "red"
unset multiplot
### end of code
Result:
I am new to Gnuplot, I have a non-linear data set and I want to fit the data within the linear range only. I normally do the fitting and specifies the fit range using the following command and redo the fitting process by changing the fit range manually until I get the optimum range for the fit:
fit [0.2:0.6]f(x) "data.txt" u 2:3:6 yerror via m1,m2
plot "<(sed -n '15,500p' data.txt)" u 2:3:6 w yerr title 'Window A',[0:.6] f(x) notitle lc rgb 'black'
Is it possible to iteratively run the fit within some data range to obtain the optimum data range for the fit in Gnuplot?
The data is typically like this one:
data
Your data (I named the file 'mas_data.txt') looks like the following (please always show/provide relevant data in your question).
Data: (how to plot with zoom-in)
### plotting data with zoom-in
reset session
FILE = 'mas_data.txt'
colX = 2
colY = 3
set key top left
set multiplot
plot FILE u colX:colY w lp pt 7 ps 0.3 lc rgb "red" ti "Data", \
set title "Zoom in"
set origin 0.45,0.1
set size 0.5, 0.6
set xrange [0:1.0]
plot FILE u colX:colY w lp pt 7 ps 0.3 lc rgb "red" ti "Data"
unset multiplot
### end of code
Regarding the "optimum" fitting range, you could try the following procedure:
find the absolute y-minimum of your data using stats (see help stats)
limit the x-range from this minimum to the maximum x-value
do a linear fit with f(x)=a*x+b and remember the standard error value for the slope (here: a_err)
reduce the x-range by a factor of 2
go back to 3. until you have reached the number of iteration (here: N=10)
find the minimum of Aerr[i] and get the corresponding x-range
The assumption is if the relative error (Aerr[i]) has a minimum then you will have the "best" fitting range for a linear fit starting from the minimum of your data.
However, I'm not sure if this procedure will be robust for all of your datasets. Maybe there are smarter procedures. Of course, you can also decrease the xrange in different steps. This procedure could be a starting point for further adaptions and optimizations.
Code:
### finding "best" fitting range
reset session
FILE = 'mas_data.txt'
colX = 2
colY = 3
stats FILE u colX:colY nooutput # do some statistics
MinY = STATS_min_y # minimum y-value
MinX = STATS_pos_min_y # x position of minimum y-value
Xmax = STATS_max_x # maximum x-value
XRangeMax = Xmax-MinX
f(x,a,b) = a*x + b
set fit quiet nolog
N = 10
array A[N]
array B[N]
array Aerr[N]
array R[N]
set print $myRange
do for [i=1:N] {
XRange = XRangeMax/2**(i-1)
R[i] = MinX+XRange
fit [MinX:R[i]] f(x,a,b) FILE u colX:colY via a,b
A[i] = a
Aerr[i] = a_err/a*100 # asymptotic standard error in %
B[i] = b
print sprintf("% 9.3g % 9.3f %g",MinX,R[i],Aerr[i])
}
set print
print $myRange
set key bottom right
set xrange [0:1.5]
plot FILE u colX:colY w lp pt 7 ps 0.3 lc rgb "red" ti "Data", \
for [i=1:N] [MinX:R[i]] f(x,A[i],B[i]) w l lc i title sprintf("%.2f%%",Aerr[i])
stats [*:*] $myRange u 2:3 nooutput
print sprintf('"Best" fitting range %.3f to %.3f', MinX, STATS_pos_min_y)
### end of code
Result:
Zoom-in xrange[0:1.0]
0.198 19.773 1.03497
0.198 9.985 1.09066
0.198 5.092 1.42902
0.198 2.645 1.53509
0.198 1.421 1.81259
0.198 0.810 0.659631
0.198 0.504 0.738046
0.198 0.351 0.895321
0.198 0.274 2.72058
0.198 0.236 8.50502
"Best" fitting range 0.198 to 0.810
I have a file with four columns. I have plotted 4D-plot using gnuplot tool as follows.
splot 'test.dat' u 1:2:3:($4<200.0?$4/4.184:1/0) w pm3d
Now I want to see a piece of the plot whose X-axis is some constant value. Let's say when the first column is 0.3, I want to see a 3D plot constructed out of 2,3,4 columns.
You don't show your data, so I assumed something.
Similar as you determine your color with the ternary operator you can "filter" a slice with constant x+dx.
Code:
### slice from 4D data
reset session
# create some test data
f(x,y) = x**2 + y**2
c(x,y) = x + y
set print $Data
do for [i=-10:10] {
do for [j=-10:10] {
print sprintf("%.3f %.3f %.3f %.3f", i, j, f(i,j), c(i,j))
}
print ""
}
set print
set xrange [-10:10]
set yrange [-10:10]
set zrange [0:200]
set cbrange [-20:20]
SliceX = 5
dx = 1
set multiplot layout 1,2
splot $Data u 1:2:3:4 w pm3d notitle
splot $Data u ($1>=SliceX && $1<=SliceX+dx?$1:NaN):2:3:4 w pm3d notitle
unset multiplot
### end of code
Result:
I'm pretty new to gnuplot, so I'm thankful for every advice.
Right now, I am trying to plot some data using the logscale command. But I don't know why all the xtics disappear when I use the logscale. This is the script I use:
#creates a plot of all the four different loops with a logscale. Fits the functions as well and saves the fitting data
#in a file named fitting.dat
set size 1,1
# set logscale
set logscale y 10
set logscale x 10
#set xlabel and y label
set xlabel "Dimension of Matrix"
set ylabel "time [s]"
#scale plot
set xrange [450:850]
set yrange[0.01:5]
#nothing displayed from fitting
set fit quiet
#position of legend
set key top right
set key horizontal
# guessing the parameters, the fit will be better and we know that the exponent should be \approx 3
b=3
d=3
f=3
h=3
#Define all th four different data fitting functions, asuming f(x) ~ a*x^b
f(x)= a*x**b
g(x)=c*x**d
h(x)=e*x**f
j(x)=g*x**h
#fit the different functions
fit f(x) 'matmul.txt' using 1:2 via a,b
fit g(x) 'matmul.txt' using 1:3 via c,d
fit h(x) 'matmul.txt' using 1:4 via e,f
fit j(x) 'matmul.txt' using 1:5 via g,h
# save the fitting parameters in an extra file
set print 'fitting.dat'
print 'function'
print a,'*x', '**', b , ' rows'
print c,'*x', '**', d , ' cols'
print e,'*x', '**', f , ' intrinsic function'
print g,'*x', '**', h , ' lapack routine'
# plot everything
plot "matmul.txt" u 1:2 t "rows" ,\
"matmul.txt" u 1:3 t "cols" ,\
"matmul.txt" u 1:4 t "intrinsic" ,\
"matmul.txt" u 1:5 t "lapack" ,\
f(x) t sprintf("row:%.2e*x^(%.2f)", a,b),\
g(x) t sprintf("col:%.2e*x^(%.2f)",c,d),\
h(x) t sprintf("int:%.2e*x^(%.2f)",e,f),\
j(x) t sprintf("lap:%.2e*x^(%.2f)",g,h)
#choose output format
set terminal png
set output "time.png"
replot
#now, non-logarithmic plot
#unset logscale
set yrange[0.01:1]
unset logscale
#plot again
plot "matmul.txt" u 1:2 t "rows" ,\
"matmul.txt" u 1:3 t "cols" ,\
"matmul.txt" u 1:4 t "intrinsic" ,\
"matmul.txt" u 1:5 t "lapack" ,\
f(x) t sprintf("col:%.2e*x^(%.2f)", a,b),\
g(x) t sprintf("row:%.2e*x^(%.2f)",c,d),\
h(x) t sprintf("int:%.2e*x^(%.2f)",e,f),\
j(x) t sprintf("lap%.2e*x^(%.2f)",g,h)
My Input file 'matmul.txt' looks like this:
#Dim rows cols intrinsic lapack
500 0.1320E+00 0.1040E+00 0.6800E-01 0.2000E-01
520 0.1400E+00 0.1320E+00 0.5600E-01 0.2000E-01
540 0.1480E+00 0.1400E+00 0.6000E-01 0.3200E-01
560 0.1680E+00 0.1480E+00 0.7200E-01 0.2400E-01
580 0.1800E+00 0.1680E+00 0.6800E-01 0.3200E-01
600 0.1920E+00 0.1960E+00 0.7200E-01 0.3600E-01
620 0.2080E+00 0.2040E+00 0.9600E-01 0.2000E-01
640 0.4000E+00 0.3520E+00 0.8400E-01 0.3200E-01
...
Now, If I run this file, I obtain the following output plot
I don't know why, but the range of the yscale is not correct and the xtics are not displayed. If I plot it without 'logscale', the plot is exactly what I want. Why doesn't this work?
Tics in logarithmic plots are not separated by a constant summand as in 1, 2, 3, ..., they are separated by a constant factor as in 1, 10, 100, ...
This means in your case for the y-axis: You have given the range [0.01:5], leading to tics at 0.01, 0.1, 1 as it is seen in the picture. Above 1, you have minor tics at 2, 3, 4, and 5. 5 is the upper boundary of the graph as specified in the range. To also have a label at this tic, just add it with:
set ytics add (5)
or change the yrange to one of
set yrange [0.01:1]
set yrange [0.01:10]
For your xtics: Labels would be at 1, 10, 100, 1000, ... But your range is from 450 to 850: no labeled xtic inside.
Again, you can set them manually:
set xtics (450, 550, 650, 750, 850)
Your x-axis spans less than a decade and the default major tic frequency is a decade. If you want labeled tics within this range use set xtics (400,500,600,700,800) or whatever you want.
This is all in the documentation, just search for "logscale"