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.

I am trying to plot a scatterplot with a fit line that I have already calculated, but I am having trouble getting them to line up and use the same scale. I tried doing it with

\begin{tikzpicture}[domain=0:8]
\begin{axis}[xlabel={$\Delta x$ (m)}, ylabel={$Mg$ (N)}]
    \draw[color=red, domain=0:1000] plot (\x,{0.0051026+10.372231*\x}) node[below right] {};
\addplot[scatter, only marks, scatter src=\thisrow{class},
      error bars/.cd, y dir=both, x dir=both, y explicit, x explicit, error bar style={color=mapped color}]
      table[x=x,y=y,x error=xerr,y error=yerr] {
x      xerr      y        yerr     class
0.0047 0.0007071 0.054039 0.000098 0
0.0142 0.0007071 0.152651 0.000098 0
0.0237 0.0007071 0.252051 0.000098 0
0.0332 0.0007071 0.350466 0.000098 0
0.0525 0.0007071 0.548380 0.000098 0
0.0622 0.0007071 0.646893 0.000098 0
0.0720 0.0007071 0.746195 0.000098 0
0.0802 0.0007071 0.844709 0.000098 0
};
\end{axis}
\end{tikzpicture}

but I am getting a plot that looks like

enter image description here

Can I make the fit line match up with the scatter plot with the type of plot I am using? If not, how can I make this work?

share|improve this question
    
Maybe useful: tex.stackexchange.com/q/11251/14100 –  Scott H. Oct 10 '12 at 3:15
    
That does look useful, but I'd prefer to be able to input a function that I already have –  murgatroid99 Oct 10 '12 at 3:31
    
As per the question @ScottH. linked to, adding a separate \addplot [blue, mark=none] table[y={create col/linear regression={y=y}}] { with the same data will create a linear regression line to fit the given data. If you want to use the function you already computed you need to use a separate \addplot. –  Peter Grill Oct 10 '12 at 4:03

1 Answer 1

up vote 8 down vote accepted

You have two issues with your MWE:

  • You are trying to mix tikz macros within pgfplot's axis environment.
  • Your domain for the function you are tying to plot is incorrect.

1. Manual Fit Line:

So using another \addplot and correcting the domain to 0:08:

\addplot [color=red, domain=0:0.08, mark=none] {0.0051026+10.372231*\x};

you get:

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
\begin{tikzpicture}[domain=0:8]
\begin{axis}[xlabel={$\Delta x$ (m)}, ylabel={$Mg$ (N)}]
%    \draw[color=red, domain=0:1000] plot (\x,{0.0051026+10.372231*\x}) node[below right] {};

\addplot [color=red, domain=0:0.08, mark=none] {0.0051026+10.372231*\x};

\addplot[scatter, only marks, scatter src=\thisrow{class},
      error bars/.cd, y dir=both, x dir=both, y explicit, x explicit, error bar style={color=mapped color}]
      table[x=x,y=y,x error=xerr,y error=yerr] {
x      xerr      y        yerr     class
0.0047 0.0007071 0.054039 0.000098 0
0.0142 0.0007071 0.152651 0.000098 0
0.0237 0.0007071 0.252051 0.000098 0
0.0332 0.0007071 0.350466 0.000098 0
0.0525 0.0007071 0.548380 0.000098 0
0.0622 0.0007071 0.646893 0.000098 0
0.0720 0.0007071 0.746195 0.000098 0
0.0802 0.0007071 0.844709 0.000098 0
};


\end{axis}
\end{tikzpicture}
\end{document}

2. Automatic Fit Line:

Alternatively you could let pgfplots compute the regression line for you by using the same data with:

\addplot [red, mark=none] table[y={create col/linear regression={y=y}}

enter image description here

Notes:

  • Note that in this case the regression line is automatically only between the extreme points. You would need to adjust your domain to get the same results.
  • You should not duplicate the data as I have done.

Code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
\begin{tikzpicture}[domain=0:8]
\begin{axis}[xlabel={$\Delta x$ (m)}, ylabel={$Mg$ (N)}]
%    \draw[color=red, domain=0:1000] plot (\x,{0.0051026+10.372231*\x}) node[below right] {};

\addplot[scatter, only marks, scatter src=\thisrow{class},
      error bars/.cd, y dir=both, x dir=both, y explicit, x explicit, error bar style={color=mapped color}]
      table[x=x,y=y,x error=xerr,y error=yerr] {
x      xerr      y        yerr     class
0.0047 0.0007071 0.054039 0.000098 0
0.0142 0.0007071 0.152651 0.000098 0
0.0237 0.0007071 0.252051 0.000098 0
0.0332 0.0007071 0.350466 0.000098 0
0.0525 0.0007071 0.548380 0.000098 0
0.0622 0.0007071 0.646893 0.000098 0
0.0720 0.0007071 0.746195 0.000098 0
0.0802 0.0007071 0.844709 0.000098 0
};

\addplot [red, mark=none] table[y={create col/linear regression={y=y}}] {
x      xerr      y        yerr     class
0.0047 0.0007071 0.054039 0.000098 0
0.0142 0.0007071 0.152651 0.000098 0
0.0237 0.0007071 0.252051 0.000098 0
0.0332 0.0007071 0.350466 0.000098 0
0.0525 0.0007071 0.548380 0.000098 0
0.0622 0.0007071 0.646893 0.000098 0
0.0720 0.0007071 0.746195 0.000098 0
0.0802 0.0007071 0.844709 0.000098 0
};

\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
What is the class column for? –  Magpie Jan 28 '13 at 23:21
    
@Magpie: Don't know. That was directly from the one provided in the MWE so assume that it is intended to be another column of data. –  Peter Grill Jan 29 '13 at 0:40
    
Is it possible to get the automatic regression line labelled on the the graph too automatically? And include an R^2 value? –  HCAI Mar 13 '13 at 16:45
    
@user1134241: This might be better as a separate question. Also, what do you mean by "include a R^2 value?" –  Peter Grill Mar 13 '13 at 18:07
1  
@user1134241: The line is drawn only between the extreme values (as it should be) and not extended past them, the computation is AFAIK a complete linear regression. –  Peter Grill Mar 13 '13 at 19:14

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.