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.

New to LaTeX, and was hoping someone would be kind enough to give me a hand with trying to draw lines of best fit to these data points. I have included the example I am working on.

\documentclass[]{article}
\usepackage{filecontents,pgfplots}
\usepackage{pgfplotstable}
% recommended:
\usepackage{booktabs}
\usepackage{array}
\usepackage{colortbl}
%\usepgfplotslibrary{clickable}
\pgfdeclareplotmark{halfcircle}{%
\begin{pgfscope}
\pgfsetfillcolor{white}%
\pgfpathcircle{\pgfpoint{0pt}{0pt}}
{\pgfplotmarksize}
\pgfusepathqfillstroke
\end{pgfscope}%
\pgfpathmoveto
{\pgfpoint{\pgfplotmarksize}{0pt}}
\pgfpatharc{0}{180}{\pgfplotmarksize}
\pgfpathclose
\pgfusepathqfill
}

\begin{filecontents}{conductivity.dat}
L(cm)   50mA    100mA  200mA 400mA 800mA 1000mA
0.0000  0.0000 0.0000 0.000 0.000 0.000 0.000
2.0000  0.0342 0.0635 0.128 0.260 0.520 0.650
3.0000  0.0555 0.1043 0.212 0.423 0.840 1.030
4.0000  0.1104 0.2064 0.396 0.809 1.300 1.700
6.0000  0.1350 0.2426 0.502 1.034 2.010 2.471
7.0000  0.1669 0.3326 0.673 1.339 2.610 3.140

\end{filecontents}

\begin{document}
\pgfplotstableread{conductivity.dat}{\conductivity}
\centering
\begin{tikzpicture}[scale=1]
\begin{axis}[xmin = 0, ymin = 0,
            xlabel=Bed depth (cm),
            ylabel = Voltage, 
            width=1.2\textwidth,
            height=0.8\textwidth, legend pos={north west},
            xticklabel style={/pgf/number format/1000 sep=}]
\addplot+[only marks][black, mark = otimes] table [x={L(cm)}, y={50mA}] {\conductivity};\addlegendentry{50mA}
\addplot+[only marks] [mark = star, black] table [x={L(cm)}, y={100mA}] {\conductivity};\addlegendentry{100mA}
\addplot+[only marks] [mark = o, black ] table [x={L(cm)}, y={200mA}] {\conductivity};\addlegendentry{200mA}
\addplot+[only marks] [mark = +, black] table [x={L(cm)}, y={400mA}] {\conductivity};\addlegendentry{400mA}
\addplot+[only marks] [mark = oplus, black] table [x={L(cm)}, y={800mA}] {\conductivity};\addlegendentry{800mA}
\addplot+[only marks] [mark=halfcircle, black] table [x={L(cm)}, y={1000mA}] {\conductivity};\addlegendentry{1000mA}
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this question
4  
Is this what you are looking for: tex.stackexchange.com/questions/119179/… ? –  Philipp Aug 22 '13 at 12:55
    
Thanks Phil, but I'm struggling to incorporate the regression in an in-line table. Any chance you could try applying it to the example I've provided please? –  QUAMSTAR Aug 22 '13 at 14:10
    
I'm sorry, but I'm not sure I understand what you mean by "incorporate the regression in an in-line table". Do you mean that you want to incorporate the equation of the regression line into a LaTeX table? Or do you want to let LaTeX calculate the values of the regression line for the x values in a data table? –  Philipp Aug 22 '13 at 14:19
    
more the latter...have LaTeX calculate and draw the regression line for the data points plotted. –  QUAMSTAR Aug 22 '13 at 14:26

1 Answer 1

up vote 5 down vote accepted

Does this fit your needs? I added the values of the regression lines for each voltage to the \conductivity table with the command \pgfplotstablecreatecol. Then you can use easily use those values in plots or use pgfplotstable to give you a table containing the values.

\documentclass[]{article}
\usepackage{filecontents,pgfplots}
\usepackage{pgfplotstable}
% recommended:
\usepackage{booktabs}
\usepackage{array}
\usepackage{colortbl}
%\usepgfplotslibrary{clickable}
\pgfdeclareplotmark{halfcircle}{%
\begin{pgfscope}
\pgfsetfillcolor{white}%
\pgfpathcircle{\pgfpoint{0pt}{0pt}}
{\pgfplotmarksize}
\pgfusepathqfillstroke
\end{pgfscope}%
\pgfpathmoveto
{\pgfpoint{\pgfplotmarksize}{0pt}}
\pgfpatharc{0}{180}{\pgfplotmarksize}
\pgfpathclose
\pgfusepathqfill
}

\begin{filecontents}{conductivity.dat}
L(cm)   50mA    100mA  200mA 400mA 800mA 1000mA
0.0000  0.0000 0.0000 0.000 0.000 0.000 0.000
2.0000  0.0342 0.0635 0.128 0.260 0.520 0.650
3.0000  0.0555 0.1043 0.212 0.423 0.840 1.030
4.0000  0.1104 0.2064 0.396 0.809 1.300 1.700
6.0000  0.1350 0.2426 0.502 1.034 2.010 2.471
7.0000  0.1669 0.3326 0.673 1.339 2.610 3.140

\end{filecontents}

\begin{document}
\pgfplotstableread{conductivity.dat}{\conductivity}
% create the `regression' columns:
\pgfplotstablecreatecol[linear regression={y=50mA}]{regression 50mA}{\conductivity}
\pgfplotstablecreatecol[linear regression={y=100mA}]{regression 100mA}{\conductivity}
\pgfplotstablecreatecol[linear regression={y=200mA}]{regression 200mA}{\conductivity}
\pgfplotstablecreatecol[linear regression={y=400mA}]{regression 400mA}{\conductivity}
\pgfplotstablecreatecol[linear regression={y=800mA}]{regression 800mA}{\conductivity}
\pgfplotstablecreatecol[linear regression={y=1000mA}]{regression 1000mA}{\conductivity}

\centering
\begin{tikzpicture}[scale=1]
\begin{axis}[xmin = 0, ymin = 0,
            xlabel=Bed depth (cm),
            ylabel = Voltage, 
            width=1.2\textwidth,
            height=0.8\textwidth, legend pos={north west},
            xticklabel style={/pgf/number format/1000 sep=}]
% regression
\addplot[black, no markers] table [x={L(cm)}, y={regression 50mA}] {\conductivity};\addlegendentry{50mA}
\addplot[orange, no markers] table [x={L(cm)}, y={regression 100mA}] {\conductivity};\addlegendentry{100mA}
\addplot[magenta, no markers] table [x={L(cm)}, y={regression 200mA}] {\conductivity};\addlegendentry{200mA}
\addplot[green, no markers] table [x={L(cm)}, y={regression 400mA}] {\conductivity};\addlegendentry{400mA}
\addplot[blue, no markers] table [x={L(cm)}, y={regression 800mA}] {\conductivity};\addlegendentry{800mA}
\addplot[red, no markers] table [x={L(cm)}, y={regression 1000mA}] {\conductivity};\addlegendentry{1000mA}
% data 
\addplot+[only marks][black, mark = otimes] table [x={L(cm)}, y={50mA}] {\conductivity};\addlegendentry{50mA}
\addplot+[only marks] [mark = star, black] table [x={L(cm)}, y={100mA}] {\conductivity};\addlegendentry{100mA}
\addplot+[only marks] [mark = o, black ] table [x={L(cm)}, y={200mA}] {\conductivity};\addlegendentry{200mA}
\addplot+[only marks] [mark = +, black] table [x={L(cm)}, y={400mA}] {\conductivity};\addlegendentry{400mA}
\addplot+[only marks] [mark = oplus, black] table [x={L(cm)}, y={800mA}] {\conductivity};\addlegendentry{800mA}
\addplot+[only marks] [mark=halfcircle, black] table [x={L(cm)}, y={1000mA}] {\conductivity};\addlegendentry{1000mA}
\end{axis}
\end{tikzpicture}

\pgfplotstabletypeset[columns={50mA, 100mA, 200mA, 400mA, 800mA, 1000mA}]\conductivity\\

\pgfplotstabletypeset[columns={regression 50mA, regression 100mA, regression 200mA, regression 400mA, regression 800mA}]\conductivity
\end{document}
share|improve this answer

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.