[Gnuplot] Correlation Coefficient & Linear Fit

In Gnuplot, stats can help you to do some statistical works easily. For example, correlation coefficient:

Take a look of chapter “Stats” in gnuplot manual first.

Use stats

1
stats data using 5:4 name "A"

data should be replaced with your data file or data variable.

Now, all statistics data have been generated automatically and ready to be used.

“A” is to be set as prefix.

For example, variable A_correlation is the correlation coefficient value between col 5 & 4.

Correlation Coefficient

Update: Sorry, it’s correlation coefficient, not R2.

Add a label to show correlation coefficient on the plot:

1
set label 1 sprintf("r = %4.2f",A_correlation) at graph 0.1, graph 0.85

Linear Fit

Variable Meaning
STATS_correlation correlation coefficient between x and y values
STATS_slope A corresponding to a linear fit y = Ax + B
STATS_intercept B corresponding to a linear fit y = Ax + B

So we can define a linear equation y = mx + c to draw it:

1
2
line_fit(x)=A_slope*(x)+A_intercept
plot line_fit(x) title 'Linear Fit'


[2015-01-04 日 10:54] done.
[2015-01-04 日 20:02] some fix.
[2015-01-06 火 13:37] Not R2.