3

I did some data collection and graphed this data using the pgfplots package. I created this linear regression graph using y={create col/linear regression={y=Y}}] as is described in the manual. This worked perfectly.

However, I would like to know the actual coordinates of some points on this line. Is there some sort of function that can give me the coordinates of the endpoints of this line?

  • 1
    Welcome to TeX.SX! Please have look at the welcome page to learn how to use Markdown syntax and always post a minimal working example (MWE). This would help us to understand your question. – Schweinebacke Feb 17 '17 at 10:54
  • 5
    The values a and b of the linear regression y(x) = a*x + b are stored in the (global) variables \pgfplotstableregressiona and \pgfplotstableregressionb, so you can calculate every point on that line you want. – Stefan Pinnow Feb 17 '17 at 10:59
  • Did my trick help you to solve your question or do you need further assistance? – Stefan Pinnow Feb 19 '17 at 12:50
  • @StefanPinnow An answer? – Symbol 1 Mar 21 '17 at 14:20
2

As already mentioned in the comment below the question you can used the stored variables \pgfplotstableregressiona and \pgfplotstableregressionb to calculate all the points on the regression line you want.

Have a look at the comments on the code for more details.

% used PGFPlots v1.14
% (borrowed code from <https://tex.stackexchange.com/a/355905/95441>)
    % because there where no data to play with, I created some dummy data
    \begin{filecontents*}{MyData.dat}
        x    y
        0    2.831
        1    2.843
        2    4.580
        3    4.808
        4    6.825
        5    7.000
        6    8.611
        7    9.295
        8    9.159
        9    11.773
        10   11.923
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
    \begin{axis} [
        % some options to have a nice picture
        xmin=-1,
        xmax=10,
        ymin=-1,
        axis lines=middle,
        legend style={
            at={(axis cs:9.8,0.2)},
            anchor=south east,
        },
        legend cell align=left,
    ]
        % show the data points
        \addplot+ [only marks] table [
            x=x,
            y=y,
        ] {MyData.dat};

        % here the linear regression using the equation $y = a*x + b$
        \addplot+ [
            mark=none,
            blue,
            thick,
        ] table [
            y={create col/linear regression={y=y}}
        ] {MyData.dat};

        % here the "end point" of the line
        \addplot [
            red,
            mark=*,
            only marks,
            % to draw it, we list here exactly the coordinates we want to see
            samples at={
                % this can either be a (direct) number ...
                10
%                % ... or, if defined/used you could also use a variable
%                \pgfkeysvalueof{/pgfplots/xmax}
            },
            % with this you can show the coordinates of the point
            nodes near coords={%
                \footnotesize
                $(\pgfmathprintnumber{\pgfkeysvalueof{/data point/x}},
                \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}})$%
            },
        % and here you state the function to use for the calculation of the
        % points, which are the stored values of a and b of the above equation
        ] {\pgfplotstableregressiona * x + \pgfplotstableregressionb};

            % this defines a variable and assigns a value ...
            \pgfmathsetmacro{\xZero}{0}
            % ... which can also be used to calculate the y value
            \pgfmathsetmacro{\yZero}{
                \pgfplotstableregressiona * \xZero + \pgfplotstableregressionb
            }
        % then you can also use this to print the value or use it to draw
        % something at this point
        \node [
            circle,
            fill=green,
            inner sep=2pt,
            label={[%
                font=\footnotesize,
                green,
            ]below right:%
                $(\pgfmathprintnumber{\xZero},
                  \pgfmathprintnumber{\yZero})$},
        ] at (axis cs:\xZero,\yZero) {};

        \legend{
            data points,
            % of course the variables can also be used in the legend
            $y = \pgfmathprintnumber{\pgfplotstableregressiona} \, x
                 + \pgfmathprintnumber{\pgfplotstableregressionb}$,
            end point of line,
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.