# Plot along path in TikZ

I'm trying to automate drawing filled plots along (mostly straight) paths. A simple example of what I'm trying to achieve:

\documentclass[border=2cm]{standalone}
\usepackage{tikz}

\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[gridded,
every path/.style={thick, red ,fill, fill opacity=.5},
every plot/.style={smooth}]

\begin{scope}
\draw (0,1) -- plot[domain=0:5, shift={(0,1)}, rotate={atan(3/4)}] (\x,0.05*\x^2) -- (4,4) -- cycle;
\draw[black] (0,1) -- (4,4);
\end{scope}

\begin{scope}[xshift=5cm]
\draw (0,3) -- plot[domain=0:sqrt(17), shift={(0,3)}, rotate={atan(-1/4)}] (\x,{0.5*sin(3*\x r)}) -- (4,2) -- cycle;
\draw[black] (0,3) -- (4,2);
\end{scope}
\end{tikzpicture}
\end{document}


But how can I automate the process (maybe even for curved paths?) How can I

• Shift the plot to the last coordinate (I tried it with \pgfextractxy, but was not able to get it to work)
• Rotate it (Works quite ok with the atan(), but what about vertical paths?)
• Set the domain to the length of the path (Tried it with the let-option and the veclen command, but I got the "dimension too large"-error)
• Have it implemented, so that I can repeat it many times (e.g. \draw (A) -- plot[on path] (\x, <function>) -- (B))

Any ideas are appreciated!

## Edit

As it seems that my intentions were not very clear: At the moment I would be happy with a solution for straight paths only. (The option for curved paths would be nice to have, though) So my goal is to have a simple method plotting a function between two given points on the canvas without having to recalculate my rotation, shift and domain every time.

• How. Can. I. Plot. On. A. Curved. Path. ?.
– user156344
May 22, 2019 at 8:32
• I‘m sorry, but I don‘t understand, what you want to tell me with your comment. @JouleV May 22, 2019 at 8:48
• How can I plot a e.g. x ^2 curve on a curve?
– user156344
May 22, 2019 at 8:49
• Unfortunately I don‘t know the english equivalent but there is a Wikipedia article in German: de.m.wikipedia.org/wiki/Krummlinige_Koordinaten . You can think of the y axis moving along the path in a way that it always stands orthogonal to the tangent (x axis) May 22, 2019 at 8:54
• But ploting along curved paths is not necessary! I would also be happy with straight paths! May 22, 2019 at 8:55

This plots along straight lines.

\documentclass[tikz,border=3.14mm]{standalone}

\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[%gridded,
every path/.style={thick, red ,fill, fill opacity=.5},
every plot/.style={smooth},
plot along line/.style args={from #1 to #2}{insert
path={
let \p1=($#2-#1$),\n1={veclen(\x1,\y1)/1cm},\n2={atan2(\y1,\x1)}
in   [shift={#1},rotate=\n2,domain=0:\n1]
}}]

\begin{scope}
\draw {[plot along line=from {(0,1)} to {(4,4)}]
plot (\x,0.05*\x^2) }-- (4,4) -- cycle;
\draw[black] (0,1) -- (4,4);
\end{scope}

\begin{scope}[xshift=5cm]
\draw[plot along line=from {(0,3)} to {(4,2)}] plot (\x,{0.5*sin(3*\x r)})
coordinate (end) --cycle;
\draw[black] (0,3) -- (end);
\end{scope}
\end{tikzpicture}
\end{document}


You can also add styles that plot along curved coordinate systems, using \usepgfmodule{nonlineartransformations} \usepgflibrary{curvilinear}.

\documentclass[tikz,border=3.14mm]{standalone}

\usetikzlibrary{calc}
\usepgfmodule{nonlineartransformations}
\usepgflibrary{curvilinear}
\begin{document}
\begin{tikzpicture}
\draw [help lines] (0,0) grid (3,2);
{
\pgfsetcurvilinearbeziercurve
{\pgfpoint{0mm}{20mm}}
{\pgfpoint{11mm}{20mm}}
{\pgfpoint{20mm}{11mm}}
{\pgfpoint{20mm}{0mm}}
\makeatletter
\pgftransformnonlinear{\pgfpointcurvilinearbezierorthogonal\pgf@x\pgf@y}%
\makeatother
\draw (0,-30pt) grid [step=10pt] (80pt,30pt);
\draw[blue,thick] plot[domain=0:2.8,samples=51] ({\x},{sin(180*\x)});
}
\draw[red, very thick]
(0mm,20mm) .. controls (11mm,20mm) and (20mm,11mm) .. (20mm,0mm);
\end{tikzpicture}
\end{document}


This example is more or less taken from the pgfmanual except that I added a plot of a function. However, I do not know what precisely you expect to be done? How do you specify the coordinate system?

• This is exactly what I was searching for. I think the second example is perfect, as I wanted to plot a function along a half circle. Thank you! May 23, 2019 at 5:07