Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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]
  \addplot table [y={create col/linear regression={}}]
  {
    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}
share|improve this question
1  
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? –  Halime Sep 23 '11 at 18:30
add comment

1 Answer 1

up vote 4 down vote accepted

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.

enter image description here

\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]
  \addplot table [y={create col/linear regression={}}]
  {
    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}
  \addplot [draw=red,thick,domain=-10000:10000] (x,\slope*x+\yintercept);
  \end{axis}
\end{tikzpicture}
\end{center}
\end{document}

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.

share|improve this answer
    
That is exactly what I wanted. Yes, the last two lines were manually added. I have one last question though: How do you specify the x and y to be decimals and not in scientific notation (meaning not having 10^-2/-4 right at the ends of each axes)? Thank you! –  Halime Sep 23 '11 at 19:57
    
If you can't find that on this site it is best to ask a new question –  Peter Grill Sep 23 '11 at 21:44
    
Okay I'll do that. Again, thanks for your help! –  Halime Sep 23 '11 at 22:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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