# Gnuplot

## Linear fit (Regression) with gnuplot

```> f(x) = m*x + b
> fit f(x) 'plotdata1_00000.txt' using 1:2 via m,b

Iteration 0
WSSR        : 20032.9           delta(WSSR)/WSSR   : 0
delta(WSSR) : 0                 limit for stopping : 1e-005
lambda   : 0.814969

initial set of free parameter values

m               = 1
b               = 1
/

Iteration 1
WSSR        : 955.717           delta(WSSR)/WSSR   : -19.9611
delta(WSSR) : -19077.2          limit for stopping : 1e-005
lambda   : 0.0814969

resultant parameter values

m               = -17.086
b               = -2.65671
/

Iteration 2
WSSR        : 941.634           delta(WSSR)/WSSR   : -0.0149562
delta(WSSR) : -14.0832          limit for stopping : 1e-005
lambda   : 0.00814969

resultant parameter values

m               = -18.382
b               = -2.03949
/

Iteration 3
WSSR        : 941.634           delta(WSSR)/WSSR   : -1.45998e-008
delta(WSSR) : -1.37477e-005     limit for stopping : 1e-005
lambda   : 0.000814969

resultant parameter values

m               = -18.3833
b               = -2.03882

After 3 iterations the fit converged.
final sum of squares of residuals : 941.634
rel. change during last iteration : -1.45998e-008

degrees of freedom    (FIT_NDF)                        : 98
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 3.09976
variance of residuals (reduced chisquare) = WSSR/ndf   : 9.60851

Final set of parameters            Asymptotic Standard Error
=======================            ==========================

m               = -18.3833         +/- 1.074        (5.841%)
b               = -2.03882         +/- 0.6153       (30.18%)

correlation matrix of the fit parameters:

m      b
m               1.000
b              -0.864  1.000

> plot 'plotdata1_00000.txt', f(x)```

## Exponential fit (regression) using gnuplot

```> f(x) = A*exp(B*x)
> fit f(x) 'plotdata1_00000.txt' using 1:2 via A,B
...

After 9 iterations the fit converged.
final sum of squares of residuals : 498.221
rel. change during last iteration : -6.28782e-008

degrees of freedom    (FIT_NDF)                        : 98
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 2.25475
variance of residuals (reduced chisquare) = WSSR/ndf   : 5.08389

Final set of parameters            Asymptotic Standard Error
=======================            ==========================

A               = -3.68297         +/- 0.2443       (6.634%)
B               = 1.91544          +/- 0.08438      (4.405%)

correlation matrix of the fit parameters:

A      B
A               1.000
B               0.962  1.000

> plot 'plotdata1_00000.txt', f(x)```

## How to do colored scatter plots?

See here:

```reset
set xrange [-2:2]
set yrange [-2:2]
set pm3d
splot exp(-x*x)*exp(-y*y)```

## And how do I get gray-scaled scatter plots?

```reset
set xrange [-2:2]
set yrange [-2:2]
set pm3d
set palette gray
splot exp(-x*x)*exp(-y*y)```

## How to show colored contours?

```reset
set xrange [-2:2]
set yrange [-2:2]
set pm3d at b
splot exp(-x*x)*exp(-y*y)```

## How to generate a histogram with gnuplot?

```bin_width = 0.01
bin_number(x) = floor(x/bin_width)
print bin_number(0.05)
# will show '5'
rounded(x) = bin_width * (bin_number(x) + 0.5)
plot 'mydata.txt' u (rounded(\$1)):(1.0) t 'data' smooth frequency w boxes```