原始答案

原始答案

我想在图像下方绘制正弦波。以下是我尝试的代码:

\documentclass{article}
\usepackage{graphicx,tikz}

\tikzset{
    % Tikz style used to work in relative coordinates to the underlying picture
    % From https://tex.stackexchange.com/a/445311/141947
    use bounding box relative coordinates/.style={
        shift={(current bounding box.south west)},
        x={(current bounding box.south east)},
        y={(current bounding box.north west)}
    },
}

\begin{document}

\begin{figure}[htp!]
\centering
\begin{tikzpicture}
% Import picture and select it as bounding box
\node[use as bounding box]{\includegraphics[width = 0.4\linewidth]{Figure.png}};
\begin{scope}[use bounding box relative coordinates]
\draw[red, domain = 0.128:0.961] plot [smooth] (\x,{0.1 * sin(\x * pi/0.5 r)});

% Grid lines
% \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
% \foreach \x in {0,1,...,9} { \node [anchor=south] at (\x/10,0) {0.\x}; }
% \foreach \y in {0,1,...,9} { \node [anchor=west] at (0,\y/10) {0.\y}; }
\end{scope}
\end{tikzpicture}
\end{figure}

\end{document}

输出为:

在此处输入图片描述

从图像中可以看出,我无法获得正弦波的一个完整周期。我尝试了不同的绘制命令输入,即

\draw[red, domain = 0.128:0.961] plot [smooth] (\x,{0.1 * sin(\x * pi/0.5 r)});

期望输出

我想在图像中绘制一个完整的正弦波周期,介于x = 0.0和之间。另外,我想将此正弦波移至。这可能吗?可以找到没有正弦波的图像x = 1.0y = -0.1这里

此外,是否有任何函数以和为TikZ输入并产生波?如果有,这将非常有用,因为它提供了一个通用解决方案。wavelengthamplitudesine

答案1

原始答案

您无法获得完整正弦的问题是,您使用了一些填充\node。您应该inner sep=0pt在该节点的选项中使用以获取正弦的正确边界框坐标:

\documentclass{article}
\usepackage{graphicx,tikz}

\tikzset{
    % Tikz style used to work in relative coordinates to the underlying picture
    % From https://tex.stackexchange.com/a/445311/141947
    use bounding box relative coordinates/.style={
        shift={(current bounding box.south west)},
        x={(current bounding box.south east)},
        y={(current bounding box.north west)}
    },
}

\begin{document}

\begin{figure}[htp!]
\centering
\begin{tikzpicture}
% Import picture and select it as bounding box
\node[use as bounding box,inner sep=0pt]{\includegraphics[width = 0.4\linewidth]{example-image-duck}};
\begin{scope}[use bounding box relative coordinates]
\draw[red, domain = 0:1] plot [smooth] (\x,{0.1 * sin(\x * pi/0.5 r)});

% Grid lines
% \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
% \foreach \x in {0,1,...,9} { \node [anchor=south] at (\x/10,0) {0.\x}; }
% \foreach \y in {0,1,...,9} { \node [anchor=west] at (0,\y/10) {0.\y}; }
\end{scope}
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述


新答案

通过大量的反复试验,以下使用两步方法来定位您的正弦。

首先,我们使用相对于图像的坐标在图像中找到两个点(这样,即使你调整图像大小,这种方法仍然有效,但只适用于这一幅图像,对于其他图像,请进行大量的反复试验),我们搜索的点是 (0,0) 和 (1,1)。我称它们(bl)b奥托姆eft 和(tr)for操作r好的。

然后我们再次更改坐标,以使用找到的两个点作为参考。现在我们可以在该范围内使用图像坐标轴,然后使用 在那里简单地绘制正弦plot

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

\tikzset{
    % Tikz style used to work in relative coordinates to the underlying picture
    % (slightly altered)
    % From https://tex.stackexchange.com/a/445311/141947
    use node coordinates/.style={
      shift={(#1.south west)},
      x={(#1.south east)},
      y={(#1.north west)}
    },
    use two point coordinates/.style 2 args={
      shift={(#1)},
      x={(#1-|#2)},
      y={(#1|-#2)},
    }
}

\begin{document}

\begin{tikzpicture}
% Import picture and select it as bounding box
  \node[inner sep=0pt](img){\includegraphics[width = 0.4\linewidth]{sinewave.png}};
  \begin{scope}[use node coordinates=img]
    \coordinate(bl) at(0.0715,0.0833);
    \coordinate(tr) at(0.958,0.948);
  \end{scope}
  \begin{scope}[use two point coordinates={bl}{tr}]
    \draw[red, domain = 0:1] plot [smooth] (\x,{0.1 * sin(\x * pi/0.5 r)-0.1});
    % Grid lines
    % \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
    % \foreach \x in {0,1,...,9} { \node [anchor=south] at (\x/10,0) {0.\x}; }
    % \foreach \y in {0,1,...,9} { \node [anchor=west] at (0,\y/10) {0.\y}; }
  \end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容