Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 5 down vote accepted

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}

output

share|improve this answer
add comment

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}

enter image description here

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}

enter image description here

share|improve this answer
add comment

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:

enter image description here

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,
     ]
  \addplot table [y={create col/linear regression={}}]
  {
    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}
share|improve this answer
add comment

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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.