我制作的回归线有点问题。我需要它从零到最高点的 x 值。
这才是它实际的样子,也是我需要的样子。有人知道怎么做吗?
谢谢 :)
编辑:当然,我已经尝试过了domain=x1:x2
\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}
答案1
由于您已经计算出回归线(斜率和 y 交点),您可以直接使用这些值绘制它并定义您喜欢的任何域:
% 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}
如果你使用,代码看起来会更好\pgfplotstableread
。我想,你已经有了......