[Tex/LaTex] Trend line or line of best fit in pgfplots

pgfplots

Is there an "out of the box" way to automatically draw the trend line in pgfplots?

Below follows a basic illustration of a scatter graph with a line of best fit:

a graph with a line of best fit

Best Answer

Can be achieved using /pgfplots/table/create col/linear regression={⟨key-value-config⟩} documented in Section 4.22 of the pgfplots manual.


Here's a slightly adapted version of the example from the manual. If you want to plot data directly from a file, replace the \datatable in the \addplot command with the file name.

\documentclass{standalone}

\usepackage{pgfplots, pgfplotstable}

\pgfplotstableread{
X Y
1 1
2 4
3 9
4 16
5 25
6 36
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=outer north east]
\addplot [only marks, mark = *] table {\datatable};
\addplot [thick, red] table[
    y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatable};
\addlegendentry{$y(x)$}
\addlegendentry{%
$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}
\end{axis}
\end{tikzpicture}
\end{document}
Related Question