5

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}
\pgfplotsset{compat=newest}                       
\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:

enter image description here

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?

Thanks in advance

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

1 Answer 1

6

Here you have a possible solution:

\documentclass[dvipsnames]{article}
\usepackage{pgfplots}
\usetikzlibrary{babel,calc,backgrounds,quotes,angles,patterns,decorations.markings,intersections,arrows,arrows.meta}
\pgfplotsset{compat=newest}                       
\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}

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .