4

Is there a way to continue a path we draw using \draw with a plot, just as we do for instance also with arc or similar commands?

In the example below I have a straight line, but I'd like the plot to append directly to that line, as indicated by the red dots. I can of course do that e.g. with plot[shift={(1,1)},...], but that means that I have to repeat that second coordinate in the path, which makes it again more cumbersome to use if we want to change something later (and the second straight line segment then points to the original endpoint without the shift). So this is more a question about "ergonomically" using this plot command inside other paths. E.g., I also like using the ++ syntax to precisely extend a path a certain distance horizontally, without having to repeat the previous y-coordinate, and I was wondering whether there is something similar we can do with the plot command inside such a path.

example output

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[domain=0:4]
\draw[black!10!white, dashed] (0, 0) grid (4, 2);
\draw (0, 1) -- (1, 1) node[left] {} plot[domain=0:1, samples=100] function{sin(6.3*x)} node[right] {} -- ++(1, 0);
\end{tikzpicture}
\end{document}
1
  • 1
    A way might be to implement the plot as a \pic, i.e. put its calculation inside the \pic. A different approch might be using saveboxes. Yet another way is: a) compiling the plot separately as standalone, b) including it via \includegraphcis inside a \node, c) position said node. Commented 14 hours ago

1 Answer 1

4

TikZ supports this as a standard path operation. pgfplots is not really applicable here since you are not using that syntax any more than you are the data visualisation syntax of TikZ.

Instead, we need the syntax of the plot path operation documented in section 22 of the pgf manual.

% Source - https://tex.stackexchange.com/q/762406
% Posted by flawr
% Retrieved 2026-04-30, License - CC BY-SA 4.0

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=0:4]
  \draw[black!10!white, dashed] (0, 0) grid (4, 2);
  \draw (0, 1) -- (1, 1) node[left] {} [domain=0:1, samples=100, smooth, variable=\x] plot({\x},{sin(6.3*\x r)}) node[right] {} -- ++(1, 0);
\end{tikzpicture}
\end{document}

plot drawn as part of path operation

So if the plot is the first operation, there is no problem: we can append to the plot in the usual way. However, we cannot use ++ or + to append the plot to a previous path segment. I am not actually sure what that would mean here, given that the plot specifies absolute coordinates. Be that as it may, I don't think it is supported.

We can, however, use any of several workarounds which still give us flexibility. One is to use a shift, but not hard-code the dimensions.

% Source - https://tex.stackexchange.com/q/762406
% Posted by flawr
% Retrieved 2026-04-30, License - CC BY-SA 4.0

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=0:4]
  \draw[black!10!white, dashed] (0, 0) grid (4, 2);
  \draw (0, 1) -- (1, 1) node[left] {} coordinate(a) [shift=(a),domain=0:1, samples=100, smooth, variable=\x] plot ({\x},{sin(6.3*\x r)}) node[right] {} -- ++(1, 0);
\end{tikzpicture}
\end{document}

path -> coordinate -> shift plot -> append

Less flexibly, you could, of course, change the plot:

% Source - https://tex.stackexchange.com/q/762406
% Posted by flawr
% Retrieved 2026-04-30, License - CC BY-SA 4.0

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=0:4]
  \draw[black!10!white, dashed] (0, 0) grid (4, 2);
  \draw (0, 1) -- (1, 1) node[left] {} [domain=0:1, samples=100, smooth, variable=\x] plot ({1+\x},{1+sin(6.3*\x r)}) node[right] {} -- ++(1, 0);
\end{tikzpicture}
\end{document}

[Output as above]

In some cases, though not here, it might make sense to put the plot segment first and use edge to draw additional segments.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.