#pgfplots linear regression

1 messages · Page 1 of 1 (latest)

untold swallow
#

Do you mean something like this? I adapted the first example from the pgfplots manual, sec. 4.24,

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{width=7cm,compat=1.18}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[legend pos=outer north east]
        \addplot[draw=none] table [
        y={create col/linear regression={y=Y}}, % compute a linear regression from the input table
        ] {
            X Y
            1 1
            2 4
            3 9
            4 16
            5 25
            6 36
        };
        \xdef\slope{\pgfplotstableregressiona}
        \xdef\intersect{\pgfplotstableregressionb}
        \addplot[blue, domain=-10:10]{\slope*x + \intersect};
        \addlegendentry{%
            $\pgfmathprintnumber{\slope} \cdot x
        \pgfmathprintnumber[print sign]{\intersect}$}
    \end{axis}
\end{tikzpicture}
\end{document}
#

,texdoc pgfplots