标签与曲线对齐

标签与曲线对齐

我想将标签“Wave 1”在垂直方向上与红色曲线的中心对齐。我还希望它在水平方向上以相同的方式放置。见图。

在此处输入图片描述

\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}

答案1

有几种方法。例如,以下将绘图后的当前位置存储在tmp水平分量的节点中。然后\yoffset使用垂直坐标将其与曲线的中间垂直位置()相结合。

\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}

结果

如果曲线是第一的图片中的元素,则可以使用当前边界框来查找曲线的左中点。这样就消除了xshift魔法维度-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}

结果

如果曲线不是第一个元素,则可以保存当前边界框,然后恢复:

\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)
;

在这种情况下,可以使用一条路径来简化示例current path bounding box.west,请参阅评论的 Kpym。

另一种变化。波的左点可以轻松计算:

\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}

答案2

这是一个使用路径边界框的方法(灵感来自这个答案以及 pgfmanual 的第 15.6 节)。

\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}

在此处输入图片描述

答案3

另一种方法是保存曲线的边界框,local bounding box然后使用此节点将文本放置在所需的任何位置。

\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}

在此处输入图片描述

相关内容