gnuplot - Does `set xrange [x_min:x_max]` limit the ranged used for function fit?
Asked Answered
L

2

6

Simple question - the range drawn on a plot can be changed with the set xrange [x_min:x_max] command.

Does this command also limit the range used when fitting a function using the data fitting tools in gnuplot? Is there a way to manually specify the ranged used for function fits? (One guess might be the command every? Do I need to over-ride xrange using every?)

The reason I ask is that I am using xrange to plot outputs zoomed in on the low value x region to view transient behaviour more clearly, but I think this may be "slicing off" values from the function fitting at larger x values outside the xrange region selected?

Lightsome answered 29/6, 2015 at 22:32 Comment(0)
U
8

This is an old question, but the current answer is incorrect: the current settings of xrange does affect the range used for fitting if no explicit range is given as part of the fit command. This can be easily seen by a simple example: if you have a datafile test.dat that contains

1 1
2 2
3 3
4 4
5 6
6 8
7 10
8 12

and use a linear fit, you get

fit a+b*x "test.dat" via a,b
plot "test.dat" w p, a+b*x w l

enter image description here

and fit parameters (a,b)=(-1.42, 1.59). However, if you first set the xrange you get

set xrange [4:8]
fit a+b*x "test.dat" via a,b
plot "test.dat" w p, a+b*x w l

enter image description here

and fit parameters (a,b)=(-4,2).

This is at least the current behavior of gnuplot 5.2, but this old thread from 2009 suggests that this has been the behavior for quite some time.

Urbannai answered 10/12, 2017 at 6:35 Comment(0)
W
3

set xrange [x_min:x_max] does not affect the range used when fitting a function.

With the fit command (the same holds for plot) you can explicitly restrict the range to fit for a variable with the following syntax:

[{dummy_variable=}{<min>}{:<max>}]

For instance you can restrict range for the x axis with:

fit [min:max] f(x) "filename"
Weir answered 29/6, 2015 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.