似乎“将 x 限制在域内”不适用于线性回归(与“跳过前 n 个=”相反,但此选项不会产生所需的结果)。下面的代码应该给出第 3、第 4 和第 5 个数据点的线性回归并创建一个斜三角形(在本例中局部正好为 2)。如何解决这个问题?
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{pgfplotstable}
\usetikzlibrary{shapes.geometric, arrows,calc,decorations,decorations.pathreplacing,
patterns,angles,quotes,intersections}
\begin{filecontents*}{data.txt}
x y
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
10 20
20 20
\end{filecontents*}
\usepackage{calculator}
\LOG[10]{2.999}{\loga}
\LOG[10]{5.001}{\logb}
\begin{document}
\pgfplotstableread{data.txt}\IGAIEdata
\pgfplotstableset{
create on use/nepw_mod/.style={
create col/copy column from table={data.txt}{nepw}
}
}
\begin{tikzpicture}
\begin{loglogaxis}[
xlabel={$\omega$},
ylabel={Relative error},
log basis x=10,
/pgf/number format/precision=5
]
% \addplot[draw=none,forget plot] table[header=true,skip coords between index={0}{2}, skip coords between index={5}{12},
%y={create col/linear regression}]{data.txt} coordinate [pos=0] (A) coordinate [pos=1] (B);
\addplot[draw=none,forget plot] table[header=true, restrict x to domain=\loga:\logb,
y={create col/linear regression={y=y}}]{data.txt} coordinate [pos=0] (A) coordinate [pos=1] (B);
%
% \addplot[draw=none,forget plot] table[header=true,skip first n=3,
% y={create col/linear regression}]{data.txt} coordinate [pos=0] (A) coordinate [pos=1] (B);
\xdef\slopea{\pgfplotstableregressiona}
\draw (A) |- (B) node [pos=0.25,anchor=east]{\pgfmathprintnumber{\slopea}};
\addplot[green,mark=*] table{data.txt};
\addplot table[restrict x to domain=\loga:\logb]{data.txt};
\end{loglogaxis}
\end{tikzpicture}
\end{document}