2

I want to draw a sine wave below an image. Following is the code I tried:

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

The output is:

enter image description here

As apparent from the image, I am not able to get one complete cycle of the sine wave. I tried different with inputs for the draw command i.e.,

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

Desired output

I want to plot one complete cycle of sine wave between x = 0.0 and x = 1.0 in the image. Also, I want to shift this sine wave to y = -0.1. Is this possible? The image without the sine wave can be found here.

Furthermore, is there any function in TikZ that takes the wavelength and amplitude as input and produces a sine wave? If yes, this will be very helpful as it provides a general solution.

Aim
  • 633

1 Answers1

5

Original answer

Your issue why you don't get a complete sine is that you have some padding from the used \node. You should use inner sep=0pt in the options of that node to get the correct bounding box coordinates for your sine:

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

enter image description here


New answer

By extensive trial and error the following uses a two step approach to position your sine.

First we find two points in your image by using coordinates relative to the image (so that this still works if you resize your image, but only for this one image, happy extensive trial and error for any other image), the points we search for are (0,0) and (1,1). I called these (bl) for bottom left and (tr) for top right.

Then we change coordinates yet again to use the two found points as reference. Now we can use your images coordinate axis in that scope and simply draw your sine there by using 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 \nodeinner sep=0pt{\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}

enter image description here

Skillmon
  • 60,462
  • @ Skillmon, I think there might a misinterpretation of the question. I want the wavelength of the sine wave to be less than the length of the image as the wavelength should be equal to the length of the x-axis (I have shared the image in the question). Furthermore, is it also possible to shift the sine wave to y = -0.1? – Aim Nov 06 '21 at 11:08
  • @Aim then I have the same question as already asked in the comments: Why don't you draw the canvas in TikZ. Why do you use an external graphic in the first place? – Skillmon Nov 06 '21 at 11:19
  • @Aim See the edit. But note that I don't think this is a viable solution you should use more than once. The extensive trial and error is really no fun. – Skillmon Nov 06 '21 at 11:43
  • @ Skillmon The external image is generated using a python script. Hence, I have no choice other than importing it. – Aim Nov 06 '21 at 13:56