6

Hope you don't mind me asking this, I'm only a starter at both gnuplot and LaTeX, but I've already searched the pgfplots manual and couldn't find an answer:

Is it possible to have pgfplots calculate and output the error of its linear regression for both parameters a and b (preferrably in the legend)? I know Origin does this automatically, and it shouldn't be so hard in pgfplots, but I haven't found anything yet.

3
  • Welcome to TeX.SX. Have a look at Linear regression - trend line with pgfplots
    – Johannes_B
    Dec 5 '13 at 18:22
  • First, thank you for answering this fast. Yet I've seen this post and as far as I can see, it only explains how to do a linear regression, and doesn't solve my problem.
    – Denna
    Dec 5 '13 at 18:29
  • Hi! It would be nice if you edited your question and added a minimal working example (MWE), with a plot that contains the regression and only misses the error values. That way, people can much easier help you ;)
    – yo'
    Dec 5 '13 at 18:37
7

You can adapt the approach from Adding values to pgfplot legend:

\documentclass[11pt]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{data.dat}
1 3
2 5
3 4
4 8
5 9
6 8
7 10
8 12
9 10
10 11
\end{filecontents}



\begin{document}

\begin{figure}[h!t]
\centering
\begin{tikzpicture}

\begin{axis}[
    legend pos=north west,
    axis x line*=bottom,
    axis y line*=left,
    tick label style={font=\small},
    grid=both,
    tick align=outside, 
    tickpos=left,
    xmin=0, ymin=0
    ]

\addplot[only marks, mark size=1.8, black] file {data.dat};
    % Now call gnuplot to fit this data
    % The key is the raw gnuplot option
    % which allows to write a gnuplot script file
    \addlegendentry[]{Experiment 1}

\addplot+[raw gnuplot, red, mark=none, smooth] gnuplot {
    f(x)=a*x+b;
    % let gnuplot fit, using column 1 and 2 of the data file
    % using the following initial guesses
    a=1;
    b=1;
    set fit errorvariables;
     fit f(x) 'data.dat' using 1:2 via a,b;
         % Next, plot the function and specify plot range
         % The range should be approx. the same as the test.dat x range
     plot [x=0:10] f(x);
    set print "parameters.dat"; % Open a file to save the parameters into
    print a, a_err; % Write the parameters to file
    print b, b_err;
       };       
\addlegendentry[]{\pgfplotstableread{parameters.dat}\parameters % Open the file Gnuplot wrote
    \pgfplotstablegetelem{0}{0}\of\parameters \pgfmathsetmacro\paramA{\pgfplotsretval} % Get first element, save into \paramA
    \pgfplotstablegetelem{1}{0}\of\parameters \pgfmathsetmacro\paramB{\pgfplotsretval}
     $\pgfmathprintnumber{\paramA}\times x + \pgfmathprintnumber{\paramB}$    
}

\end{axis}
\end{tikzpicture}

\end{figure}
\centering
\pgfplotstabletypeset[
    dec sep align,
    fixed,
    columns/0/.style={
        column name=Parameter Value
    },
    columns/1/.style={
        column name=Standard Error
    }
]{parameters.dat}

\end{document}
0
1

EDIT: I missed the part where you requested the error printing. So this answer doesn't really answer the question.

You can do it without using gnuplot:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{
    compat=1.9,
    legend style={font=\footnotesize}
}
\begin{document}
\pgfplotstableread{
    X Y
    1 1 
    2 4 
    3 3
    4 7
}{\datatable}
\begin{tikzpicture}
    \begin{axis}[
        xlabel={Some xlabel},
        ylabel={Some yabel},
        legend cell align=left,
        legend pos=north west]
        \addplot[only marks] table {\datatable};
        \addlegendentry{some legend}
        \addplot table [y={create col/linear regression={y=Y}}]
        {\datatable};
        \addlegendentry{%
        $\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
        \pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$ lin. Regression} %
    \end{axis}
\end{tikzpicture}
\end{document}

The code is adapted from Linear regression - trend line with pgfplots but slightly changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.