# How to rotate a node so it'll be parallel to a line?

I'm trying to put a node rotated parallel to a line y = 5 + x. Obviously, I expected the angle of rotation would be 45.

\documentclass[dvipsnames]{article}
\usepackage{pgfplots}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xmin = -1,
ymin = -5,
xmax = 10,
ymax = 14,
domain = -1:10,
xtick = {1,2,...,9},
ytick = \empty,
xlabel style={below right},
ylabel style={above left},
x tick label style={below},
xlabel = {$x$},
ylabel = {$f$}
]
\addplot[very thick, domain=0:11] {5 + x};
\draw node[above,rotate=45] at (4, 9) {$v = v_0 + at$};
\end{axis}
\end{tikzpicture}
\end{document}


The result is:

I've played with different angles and find appropriate one: 27. But it depends on ymin/ymax of axis environment. How to make tikz draw node at right angle (so it's parallel to the line) with auto correction to ymin/ymax?

• It's you line which, for some reason, doesn't have the polar angle it should have. Commented Nov 11, 2020 at 11:40

Here you have a possible solution:

\documentclass[dvipsnames]{article}
\usepackage{pgfplots}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
xmin = -1,
ymin = -5,
xmax = 10,
ymax = 14,
domain = -1:10,
xtick = {1,2,...,9},
ytick = \empty,
xlabel style={below right},
ylabel style={above left},
x tick label style={below},
xlabel = {$x$},
ylabel = {$f$}
]
\addplot[very thick, domain=0:11] {5 + x};
\path (axis cs:0,5)-- node[above,sloped] {$v = v_0 + at$} (axis cs:9,14);
\end{axis}
\end{tikzpicture}
\end{document}


• For those just skimming, put sloped instead of rotate=xxx. Works great! Commented Jul 25, 2022 at 20:11