5

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?

6

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

| improve this answer | |
4

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

| improve this answer | |
2

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}
| improve this answer | |
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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