7

I'm not too sure about what mistake I made, but all my linear regression lines have the same equation although they obviously are different lines. Could someone please help me? Thank you.

\documentclass{article}

\usepackage{pgfplots, pgfplotstable}
\usepackage{tikz}
\usepackage{graphicx} 
\usepackage{xcolor}


\begin{document}

\pgfplotstableread{
X Y
0 33.3
0.2 20
0.4 16.7
0.6 0
0.8 0
1 -20
}\datatable

\pgfplotstableread{
X Y
0 33.3
0.2 0
0.4 0
0.6 -20
0.8 -66.6
1 -71.4
}\datatablee

\pgfplotstableread{
X Y
0 11.5
0.2 6.9
0.4 -22.7
0.6 -26.7
0.8 -34.5
1 -37
}\datatableee

\pgfplotstableread{
X Y
0 39.6
0.2 16.67
0.4 5
0.6 -11.4
0.8 -19.6
1 -30.5
}\datatableeee

\begin{figure}
\begin{center}
\hspace{13mm}\textbf{Percent Change vs Sucrose Concentration}

\begin{tikzpicture}

\begin{axis}[legend style={at={(0.5,-0.1)},anchor=north},
xlabel={Concentration of Sucrose},
ylabel={Percent Change of Mass}]

\addplot [only marks, mark = *,red!80!black] table {\datatable};
\addplot [thick, red] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatable};
\addlegendentry{Apple}
\addlegendentry{%
$y=$$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}

\addplot [only marks, mark = *,blue!80!black] table {\datatablee};
\addplot [thick, blue] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatablee};
\addlegendentry{Celery}
\addlegendentry{%
$y=$$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}

\addplot [only marks, mark = *,green!80!black] table {\datatableee};
\addplot [thick, green] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatableee};
\addlegendentry{Russet Potato}
\addlegendentry{%
$y=$$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}

\addplot [only marks, mark = *,magenta!90!black] table {\datatableeee};
\addplot [thick, magenta] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatableeee};
\addlegendentry{Red Potato}
\addlegendentry{%
$y=$$\pgfmathprintnumber{\pgfplotstableregressiona} \cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$}

\end{axis}
\end{tikzpicture}
\end{center}

\caption{}
\end{figure}
\end{document}
6

Since you are using many plots, the \pgfplotstableregressiona is over written and the last one is displayed in all equations.

Use some thing like \xdef\slope{\pgfplotstableregressiona} then the value is stored in \slope globally. Do similarly for \pgfplotstableregressionb as below:

\documentclass{article}

\usepackage{pgfplots, pgfplotstable}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{xcolor}


\begin{document}

\pgfplotstableread{
X Y
0 33.3
0.2 20
0.4 16.7
0.6 0
0.8 0
1 -20
}\datatable

\pgfplotstableread{
X Y
0 33.3
0.2 0
0.4 0
0.6 -20
0.8 -66.6
1 -71.4
}\datatablee

\pgfplotstableread{
X Y
0 11.5
0.2 6.9
0.4 -22.7
0.6 -26.7
0.8 -34.5
1 -37
}\datatableee

\pgfplotstableread{
X Y
0 39.6
0.2 16.67
0.4 5
0.6 -11.4
0.8 -19.6
1 -30.5
}\datatableeee

\begin{figure}
\begin{center}
%\hspace{13mm}\textbf{Percent Change vs Sucrose Concentration}

\begin{tikzpicture}

\begin{axis}[legend style={at={(0.5,-0.1)},anchor=north},
xlabel={Concentration of Sucrose},
ylabel={Percent Change of Mass},
title=\textbf{Percent Change vs Sucrose Concentration}]

\addplot [only marks, mark = *,red!80!black] table {\datatable};
\addplot [thick, red] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatable};
\xdef\slope{\pgfplotstableregressiona}
\xdef\ycepte{\pgfplotstableregressionb}
\addlegendentry{Apple}
\addlegendentry{%
$y=$$\pgfmathprintnumber{\slope} \cdot x
\pgfmathprintnumber[print sign]{\ycepte}$}

\addplot [only marks, mark = *,blue!80!black] table {\datatablee};
\addplot [thick, blue] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatablee};
\xdef\slopee{\pgfplotstableregressiona}
\xdef\ycept{\pgfplotstableregressionb}
\addlegendentry{Celery}
\addlegendentry{%
$y=$$\pgfmathprintnumber{\slopee} \cdot x
\pgfmathprintnumber[print sign]{\ycept}$}

\addplot [only marks, mark = *,green!80!black] table {\datatableee};
\addplot [thick, green] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatableee};
\xdef\slopeee{\pgfplotstableregressiona}
\xdef\yceptee{\pgfplotstableregressionb}
\addlegendentry{Russet Potato}
\addlegendentry{%
$y=$$\pgfmathprintnumber{\slopeee} \cdot x
\pgfmathprintnumber[print sign]{\yceptee}$}

\addplot [only marks, mark = *,magenta!90!black] table {\datatableeee};
\addplot [thick, magenta] table[
y={create col/linear regression={y=Y}}
] % compute a linear regression from the input table
{\datatableeee};
\xdef\slopeeee{\pgfplotstableregressiona}
\xdef\ycepteeee{\pgfplotstableregressionb}
\addlegendentry{Red Potato}
\addlegendentry{%
$y=$$\pgfmathprintnumber{\slopeeee} \cdot x
\pgfmathprintnumber[print sign]{\ycepteeee}$}

\end{axis}
\end{tikzpicture}
\end{center}

\caption{}
\end{figure}
\end{document}

enter image description here

| improve this answer | |

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.