# PGFPlots Line of Best Fit Scale and Inversion Error

I am attempting to draw a linear regression line on my graph using pgfplotstable. However, for some reason, it appears to be calculating the regression three orders of magnitude too large and inverted, is this a bug or am I doing something wrong?

My data-set:

lnPa    1/T
6.5723  0.002304
6.7214  0.002273
6.7178  0.002273
6.7117  0.002278
6.7178  0.002278
6.6783  0.002283
6.6490  0.002288
6.6254  0.002294
6.6026  0.002299
6.5820  0.002304
6.5539  0.002309
6.4907  0.002320
6.4281  0.002331
6.3509  0.002347
6.2823  0.002364
6.2066  0.002381
6.1312  0.002381
6.0707  0.002404
6.0113  0.002421
5.8861  0.002445
5.8141  0.002506
5.7236  0.002475
5.6490  0.002494
5.5759  0.002506
5.5134  0.002519
5.3660  0.002545
5.2679  0.002571
5.1761  0.002584
5.0876  0.002597
5.0239  0.002611
4.9628  0.002625
4.8283  0.002646
4.7791  0.002660

It plots a graph like this without the regression line:

However, when I add in the line of best fit:

The figure for this is as below:

\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{axis}[legend pos=north east,anchor=west, xlabel={$T^{-1}/ K^{-1}$}, ylabel={$\ln P*_a$}]
\addplot [no markers, thick, red] table [y={create col/linear regression={y=lnPa}}]{GraphData.txt};
%\addlegendentry{$\dfrac{dy}{dx} = \pgfplotstableregressiona$}
%\addlegendentry{$y_{intercept} = \pgfplotstableregressionb$}
\end{axis}
\end{tikzpicture}
\caption{Graph to show the relationship between the natural log of pressure and the inverse of temperature}
\label{fig:ResultsGraph}
\end{figure}

Can anyone spot what's going on here?

• 2*10^(-3) is almost zero if you compare with 5... But it is not zero if you compare with 3*10^(-3)... So, the result is what you have to expect – koleygr Oct 23 '17 at 15:31
• Right, but why is this package scaling properly until I try and add the line of best fit which should be of the same scale ? – Persistence Oct 23 '17 at 15:32
• I posted an answer (but it is just a solution.... I am looking to find why it reads the data with opposite order than the given) – koleygr Oct 23 '17 at 15:58
• I found the reason and added in my post – koleygr Oct 23 '17 at 16:03

Here is the solution:

\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}
\begin{filecontents}{GraphData.txt}
lnPa    1/T
6.5723  0.002304
6.7214  0.002273
6.7178  0.002273
6.7117  0.002278
6.7178  0.002278
6.6783  0.002283
6.6490  0.002288
6.6254  0.002294
6.6026  0.002299
6.5820  0.002304
6.5539  0.002309
6.4907  0.002320
6.4281  0.002331
6.3509  0.002347
6.2823  0.002364
6.2066  0.002381
6.1312  0.002381
6.0707  0.002404
6.0113  0.002421
5.8861  0.002445
5.8141  0.002506
5.7236  0.002475
5.6490  0.002494
5.5759  0.002506
5.5134  0.002519
5.3660  0.002545
5.2679  0.002571
5.1761  0.002584
5.0876  0.002597
5.0239  0.002611
4.9628  0.002625
4.8283  0.002646
4.7791  0.002660
\end{filecontents}

\begin{document}

\begin{tikzpicture}
\begin{axis}[legend pos=north east,anchor=west, xlabel={$T^{-1}/ K^{-1}$}, ylabel={$\ln P*_a$}]
y={create col/linear regression={y={lnPa}}}
] % compute a linear regression from the input table
{GraphData.txt};
%\addlegendentry{$\dfrac{dy}{dx} = \pgfplotstableregressiona$}
%\addlegendentry{$y_{intercept} = \pgfplotstableregressionb$}
\end{axis}
\end{tikzpicture}
\end{document}

Result:

I just realized that it was plotting the axes in deferent order. The reason was that your x axis numbers was at the second column and so you have to give the x axis name of the column before ask for linear regression of y.

PS: The truth is that the image is "stolen" from @StefanPinnow that answered independent from me at the same time and probably knew the reason before I discover it today.

• @StefanPinnow, I "borrowed" your image... – koleygr Oct 23 '17 at 18:10
• Thanks, for simplicity's sake I just reversed the column order in my text file. I would've thought that it would have been able to work out "there are two columns, he's specified the first is 'Y' so the other must be 'X'" but hey ho. Thanks for the help! This was driving me nutty – Persistence Oct 24 '17 at 9:52