# How to get the domain for the line of best fit w/pgfplots to be all real numbers?

Although I already worked around this problem by finding the x-intercept with the linear regression equation I calculated w/gnuplot, I'm still curious to know how pgfplots can do it without me manually computing it.

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern,tikz,pgfplots,pgfplotstable}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[axis on top=false, axis x line=middle, axis y line=middle,xlabel=$\mathrm{\frac{1}{[S]}}$,ylabel=$\mathrm{\frac{1}{\textit{V}_0}}$,
xmin=-10000,xmax=10000,ymin=-0.01,ymax=0.035]
{
X        Y
10000    0.030
5000     0.02
2000     0.014
1000     0.012
500      0.0110
200      0.0104
100      0.0102
50       0.010
20       0.01
10       0.01
5        0.01
-4999.85 0
-9995.35 -0.01
};
\xdef\slope{\pgfplotstableregressiona}
\xdef\slope{\pgfplotstableregressionb}
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

• Not sure what you mean by "manually"? There are numerous math functions that can probably be used to compute the line of best fit so that if the points change you don't need to adjust your code. If you post a MWE we can help further. – Peter Grill Sep 23 '11 at 18:17
• See Section 4.22 Fitting Lines - Regression in the pgfplots manual. – Peter Grill Sep 23 '11 at 18:27
• @PeterGrill: As it can be seen in the MWE, I had to find the x-intercept. I don't want to have to plot that point on the table myself but instead let pgfplots do it for me since I want to generate a line of best fit with the data I already have. I want the linear regression line to include all real numbers in the domain, not just the data points. Does that make sense? – Miriam Sep 23 '11 at 18:30

The linear regression line is only valid for the domain of the given data, and hence by default it is only drawn from the minimum and maximum x in the given data.

If you want to extrapolate for points outside the domain of the data, you can use \pgfplotstableregressiona and \pgfplotstableregressionb to draw the best fit line and specify the desired domain.

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern,tikz,pgfplots,pgfplotstable}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
axis on top=false, axis x line=middle, axis y line=middle,
xlabel=$\mathrm{\frac{1}{[S]}}$,
ylabel=$\mathrm{\frac{1}{\textit{V}_0}}$,
xmin=-10000,xmax=10000,ymin=-0.01,ymax=0.035]
{
X        Y
10000    0.030
5000     0.02
2000     0.014
1000     0.012
500      0.0110
200      0.0104
100      0.0102
50       0.010
20       0.01
10       0.01
5        0.01
%    -4999.85 0     % Assuming that these were manually entered
%    -9995.35 -0.01
};
\xdef\slope{\pgfplotstableregressiona}
\xdef\yintercept{\pgfplotstableregressionb}

I commented out the last two lines that I assume you manually added, and corrected the second \xdef as that is the y-intercept and not the slope.