# Plot Continuous Line With 2 Points

Is there any way to plot a continuous line with pgfplot/tikz using only 2 points (i.e. it will extend past those points on its own) within the domain I specify?

-

You can use shorten with a negative length to extend the edge.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (a) {A};
\node [right of=a,node distance=3em] (b) {B};
\draw [shorten >=-3em,shorten <=-3em] (a) edge (b);
\end{tikzpicture}
\end{document}


-

With PSTricks.

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-eucl}

\begin{document}
\begin{pspicture}[showgrid=false](6,4)
\pstGeonode[PosAngle=-90](2,2){A}(4,2){B}
\pstLineAB[nodesep=-1]{A}{B}
\end{pspicture}
\end{document}


## Miscellaneous

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-eucl}

\begin{document}
\multido{\r=.0+-0.2}{11}{%
\begin{pspicture}[showgrid=false](6,4)
\pstGeonode[PosAngle=-90](2,2){A}(4,2){B}
\pstLineAB[nodesep=\r]{A}{B}
\end{pspicture}}
\end{document}


-

You can also use linear regression to compute the line for you, and draw it for the ddesire domain. The code below provides specifies the points (1,1) and (3,5) and plots the line between them for the domain=-5:5:

## Notes:

• This may be overkill depending on your application, but this solution provides an actual "graph" of the line between the two points (which may or may not be desirable).
• Note that this does not seem to work for vertical lines, so that will require special handling.

## Code:

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

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle, axis y line=middle,
xlabel=$x$,
ylabel=$y$,
grid,
]
{
X    Y
1    1
3    5
};
\xdef\slope{\pgfplotstableregressiona}
\xdef\yintercept{\pgfplotstableregressionb}
\addplot [draw= red, ultra thick, domain=-5:5] (x,\slope*x+\yintercept);
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

-

You can use another way with tikzlibraly calc to extend the line.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node (a) {A};
\node [right of=a,node distance=3em] (b) {B};
\draw ($(a)!-1.5em!(b)$) -- ($(a)!4.5em!(b)$);
\end{tikzpicture}
\end{document}

-