![pgfplots:将回归线延伸到表格点之外](https://linux22.com/image/299466/pgfplots%EF%BC%9A%E5%B0%86%E5%9B%9E%E5%BD%92%E7%BA%BF%E5%BB%B6%E4%BC%B8%E5%88%B0%E8%A1%A8%E6%A0%BC%E7%82%B9%E4%B9%8B%E5%A4%96.png)
如何延长在 中绘制的回归线pgfplots
?我想将线延长至略低于绘图边界顶部且略高于绘图边界底部的位置。
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat = 1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid = major,
xmin = -2,
xmax = 4,
ymin = -2,
ymax = 4,
xlabel = {$x$},
ylabel = {$y$},
legend pos = outer north east
]
\addplot[only marks, red] table {
X Y
0 0
1 1
2 2
2 3
};
\addplot[no marks] table [
y = {create col/linear regression = {y = Y}}
]
{
X Y
0 0
1 1
2 2
2 3
};
\addlegendentry{$y(x)$}
\addlegendentry{
$\pgfmathprintnumber{\pgfplotstableregressiona}\cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$
}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
观察@Peter Grill 的评论,一种解决方法是使用获得的数据并扩展绘图。
\pgfplotstableregressiona*x+\pgfplotstableregressionb
代码
\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots,filecontents}
\usepackage{pgfplotstable}
\pgfplotsset{compat = newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid = major,
xmin = -2,
xmax = 4,
ymin = -2,
ymax = 4,
xlabel = {$x$},
ylabel = {$y$},
legend pos = outer north east
]
\addplot[only marks, red] table {
X Y
0 0
1 1
2 2
2 3
};
\addplot[no marks] table [
y = {create col/linear regression = {y = Y}}
]
{
X Y
0 0
1 1
2 2
2 3
};
\addlegendentry{$y(x)$}
\addlegendentry{
$\pgfmathprintnumber{\pgfplotstableregressiona}\cdot x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$
}
\addplot[no marks,red,domain=-1:0]{\pgfplotstableregressiona*x+\pgfplotstableregressionb};
\addplot[no marks,red,domain=2:3]{\pgfplotstableregressiona*x+\pgfplotstableregressionb};
\end{axis}
\end{tikzpicture}
\end{document}