1
votes

I am plotting two graphs using gnuplot. First plot is actual data and the second one is the fitting of the data.

The script I used for plotting this is shown here below:

#!/usr/bin/gnuplot

reset 


set terminal png enhanced
set terminal pngcairo enhanced color dashed
set output 'msd-maltoLyo12per-225ns.png'

##########################################
set macros

labelSIZE="font 'Arial,24'"
ticFONT="font 'Arial,16"
set key font 'Arial,14'
set key spacing 1.5 samplen 5 
##########################################

set xrange [0:225]  
set yrange [0:11000]


set xtic @ticFONT
set ytic @ticFONT
set xtics out nomirror
set ytics out nomirror
##############################

set style line 1 lt 1 lc rgb "red"  lw 2.0 
set style line 2 lt 2 lc rgb "blue"     lw 2.0 
set style line 3 lt 3 lc rgb "coral"    lw 2.0 
set style line 4 lt 4 lc rgb "green"    lw 2.0 
set style line 5 lt 5 lc rgb "black"    lw 2.0 

##############################


f(x)=a+b*x
fit [120:225] f(x) 'diff-xy-maltoLyo12per.dat' via a,b

plot    'diff-xy-maltoLyo12per.dat' using 1:2 with lines linestyle 1 title "{/Symbol b}Mal-C_{12}", f(x) lw 3.0 lc rgb 'black' 

Here I plot the fitting graph from 1 to 120 as shown here. Also I want to plot the same graph from range 120 to 225 as in the picture here.

Now I want a single plot which contain the two black lines and the red line.

How can I achieve this?

Thanks in advance.

1

1 Answers

2
votes

Working with the script you already have, you can use two functions to fit in the different ranges separately, and then use a conditional plot that selects one if x < 120 and the other one if x > 120:

f1(x)=a1+b1*x
fit [0:120] f1(x) 'diff-xy-maltoLyo12per.dat' via a1,b1

f2(x)=a2+b2*x
fit [120:225] f2(x) 'diff-xy-maltoLyo12per.dat' via a2,b2

f(x) = x < 120 ? f1(x) : f2(x)

plot    'diff-xy-maltoLyo12per.dat' using 1:2 with lines linestyle 1 title "{/Symbol b}Mal-C_{12}", f(x) lw 3.0 lc rgb 'black' 

Now, the way I would go about this, would be to generate a special fitting function, whose parameters would give me the point at which the slope changes as a result of the fitting itself. Say you call that point x0 (for which the value of the function is y0), the slope at the left of it is m1 and the slope at the right m2. Then the function at the left has the form m1*(x-x0)+y0 and the function at the right has the form m2*(x-x0)+y0. The overall function can be defined in gnuplot as:

f(x) = x < x0 ? m1*(x-x0)+y0 : m2*(x-x0)+y0

and you can fit f(x) "data" via x0, m1, m2, y0. You can also generate this function without the condition using a step function:

f(x) = m1*(x-x0)*(sgn(x0-x)+1)/2 + m2*(x-x0)*(sgn(x-x0)+1)/2 + y0

After you fit, for which you might need to provide some initial values, you can print x0 and you'll get the best value (that should be close to 120 in your case, as you know) for the position of the change in slope.