5

I would like to align the label "Wave 1" to the center of the red curve in the vertical direction. I still want it to be placed the same way in the horizontal direction. See the picture.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
\newcommand\scale{0.3}

\path[use as bounding box] (0,8) rectangle (7,10);

\draw[scale=0.5,domain=3:8,smooth,variable=\x,red] plot ({\x+1}, 
{\scale*sin(deg(\x+4))+17}) node[anchor=east,xshift = -2.5cm ] {Wave 1}
\end{tikzpicture}
\end{document}
BambOo
  • 8,801
  • 2
  • 20
  • 47
Jesper H
  • 329
  • Try going theother way \draw[red] node[inner sep=0] (w1) {Wave 1} {[shift={(w1.east)}] plot[domain=0:0.97*pi] (\x, {0.2*sin(1.5*(0.15*pi+\x) r)}) }; – percusse Apr 01 '18 at 10:36

3 Answers3

6

There are several ways. For example, the following stores the current position after the plot in the node tmp for the horizontal component. Then it is combined with the middle vertical position (\yoffset) of the curve by using perpendicular coordinates.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
  \newcommand\scale{0.3}
  \newcommand\yoffset{17}

  \draw[scale=0.5, domain=3:8, smooth, variable=\x, red]
    plot ({\x+1}, {\scale*sin(deg(\x+4)) + \yoffset})
    coordinate (tmp) (tmp |- 0, \yoffset)
    node[anchor=east,xshift = -2.5cm] {Wave 1}
  ;
\end{tikzpicture}
\end{document}

Result

If the curve is the first element in the picture, then the current bounding box can be used to find the left middle point of the curve. This gets rid of xshift and the magic dimension -2.5cm.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
  \newcommand\scale{0.3}
  \newcommand\yoffset{17}

  \draw[scale=0.5, domain=3:8, smooth, variable=\x, red]
    plot ({\x+1}, {\scale*sin(deg(\x+4)) + \yoffset})
    (current bounding box.west)
    node[anchor=east] {Wave 1}
  ;
\end{tikzpicture}
\end{document}

Result

If the curve is not the first element, then the current bounding box can be saved, and restored afterwards:

\draw[scale=\plotScale, domain=3:8, smooth, variable=\x, red]
  % Save current bounding box and clear the box
  (current bounding box.south west) coordinate (bbll)
  (current bounding box.north east) coordinate (bbur)
  \pgfextra{\pgfresetboundingbox}
  %
  plot ({\x+1}, {\scale*sin(deg(\x+4)) + \yoffset})
  (current bounding box.west)
  node[anchor=east] {Wave 1}
  %
  % Add old bounding box to current bounding box.
  (bbll) (bbur)
;

In this case, with one path, the example can be simplified by using current path bounding box.west, see the comment of Kpym.

Another variation. The left point of the wave can be easily calculated:

\begin{tikzpicture}
  \newcommand\scale{0.3}
  \newcommand\yoffset{17}
  \newcommand\plotScale{.5}
  \newcommand\domainMin{3}
  \newcommand\domainMax{8}

  \draw[
    scale=\plotScale,
    domain=\domainMin:\domainMax,
    smooth,
    variable=\x,
    red,
  ]
    plot ({\x+1}, {\scale*sin(deg(\x+4)) + \yoffset})
    (\domainMin + 1, \yoffset)
    node[anchor=east] {Wave 1}
  ;
\end{tikzpicture}
Heiko Oberdiek
  • 271,626
3

Here is a method that uses the bounding box of the path (inspired by this answer and section 15.6 of the pgfmanual).

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\tikzset{mark path extrema/.style = {
    path picture={
      \coordinate (#1-bl) at (path picture bounding box.south west);
      \coordinate (#1-tr) at (path picture bounding box.north east);
      \coordinate (#1-br) at (path picture bounding box.south east);
      \coordinate (#1-tl) at (path picture bounding box.north west);
    }
  }
}


\begin{document}

\begin{tikzpicture}
\newcommand\scale{0.3}

\path[use as bounding box] (0,8) rectangle (7,10);

\draw[scale=0.5,domain=3:8,smooth,variable=\x,red,mark path extrema=wave] plot ({\x+1}, 
{\scale*sin(deg(\x+4))+17}); 
\node[anchor=east] at ($(wave-tl)!0.5!(wave-bl)$){Wave 1};
\end{tikzpicture}
\end{document}

enter image description here

2

Another way of doing this is to save the bounding box of your curve with local bounding box and then use this node to position your text wherever you want.

\documentclass[tikz,border=7pt]{standalone}
\newcommand\scale{0.3}
\begin{document}
  \begin{tikzpicture}
    \draw[scale=0.5, domain=3:8, smooth,variable=\x, red, local bounding box = wave]
      plot ({\x+1},{\scale*sin(deg(\x+4))+17});
    \node[right] at (wave.east) {Wave on East};
    \node[left] at (wave.west) {Wave on West};
    \draw[opacity=.1,step = 2pt] (wave.south east) grid (wave.north west);
  \end{tikzpicture}
\end{document}

enter image description here

Kpym
  • 23,002