3

I have a little Problem with a regression line I made. I need it to be from its zero up to the x-value of the highest point.

This is how it actually looks und underneath how I need it. Does anyone know how to do this?

Thank you :)

edit: of course, I already tried out domain=x1:x2

enter image description here

enter image description here

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

\begin{document}
\sffamily

\pgfplotsset{every axis legend/.append style={
at={(0.03,0.97)},
anchor=north west}}
\begin{tikzpicture}
\begin{axis}[
    width=10cm,
    height=10cm,
    x tick label style={/pgf/number format/1000 sep=},
    xmin=0, xmax=0.6, ymin=0, ymax=22, 
    xlabel=Weg\,/\,mm, 
    ylabel=Kraft\,/\,N,
    ]

\pgfplotstableread{data.txt}
\datatable


\addplot+[
    color = blue,
    fill = blue,
    mark = *,
    mark options={solid},
    only marks,
    ] table {
x       y
0.400   18.096
0.400   18.401
0.400   17.767
0.400   16.992
0.400   16.399
0.397   15.908
0.392   15.440
0.387   14.533
0.382   13.258
0.377   11.939

};
\addlegendentry{Kraft-Weg-Kurve}

\addplot [
    domain=0.25:0.4
    no markers,
    ] table [
        y={create col/linear regression={y=y}}] {
x       y
0.400   18.096
0.400   18.401
0.400   17.767
0.400   16.992
0.400   16.399
0.397   15.908
0.392   15.440
0.387   14.533
0.382   13.258
0.377   11.939
};
\addlegendentry{Regression {$\pgfmathprintnumber[precision=4, fixed zerofill]{\pgfplotstableregressiona} \cdot \mathrm{t} + \pgfmathprintnumber[precision=4, fixed zerofill]{\pgfplotstableregressionb}$}}




\end{axis}
\end{tikzpicture}


\end{document}
simande
  • 1,002

1 Answers1

4

As you already have calculated your regression line (slope and y-intersection) you can plot it directly with those values and define whatever domain you like:

% arara: pdflatex

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{%
    ,compat=1.12
    ,every axis legend/.append style={%
        ,at={(0.03,0.97)}
        ,anchor=north west}
        }
\usepackage{pgfplotstable}

\begin{document}
\sffamily   
\begin{tikzpicture}
    \begin{axis}[%
        ,width=10cm,height=10cm
        ,x tick label style={/pgf/number format/1000 sep=}
        ,xmin=0,xmax=0.6
        ,ymin=0,ymax=22 
        ,xlabel=Weg/mm, 
        ,ylabel=Kraft/N,
        ]
        \addplot+[%
            ,fill = blue
            ,mark = *
            ,mark options={solid}
            ,only marks
            ] table {%
                x       y
                0.400   18.096
                0.400   18.401
                0.400   17.767
                0.400   16.992
                0.400   16.399
                0.397   15.908
                0.392   15.440
                0.387   14.533
                0.382   13.258
                0.377   11.939      
                };
        \addlegendentry{Kraft-Weg-Kurve}
        \addplot [] table [%
            ,y={create col/linear regression}] {%
                x       y
                0.400   18.096
                0.400   18.401
                0.400   17.767
                0.400   16.992
                0.400   16.399
                0.397   15.908
                0.392   15.440
                0.387   14.533
                0.382   13.258
                0.377   11.939
                };  
        \addplot [draw,domain=0.25:0.5] (x,\pgfplotstableregressiona*x+\pgfplotstableregressionb);
        \addlegendentry{Regression {$\pgfmathprintnumber[precision=4, fixed zerofill]{\pgfplotstableregressiona} \cdot \mathrm{t} + \pgfmathprintnumber[precision=4, fixed zerofill]{\pgfplotstableregressionb}$}}  
    \end{axis}
\end{tikzpicture}   
\end{document}

enter image description here

The code looks nicer, if you use \pgfplotstableread. I guess, you had that already...

LaRiFaRi
  • 43,807