0
votes

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?

1

1 Answers

0
votes

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