# Use tikz to plot sine wave with text and lines

I wanted to get a tikz plot to look like:

I am getting two cycles in my code. The original code was masterfully written by moospit found here. Here is the code to start with:

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{datavisualization.formats.functions, arrows}

\def\mytypesetter#1{
\pgfmathparse{#1/pi}
\pgfkeys{/pgf/number format/precision=2}
\pgfmathroundtozerofill{\pgfmathresult}
\pgfmathifisint{\pgfmathresult}{
\pgfmathprintnumber{\pgfmathresult}$\pi$
}{
\pgfmathprintnumber[/pgf/number format/frac, frac whole=false]{\pgfmathresult}$\pi$
}
}

\begin{document}
\begin{tikzpicture}
\datavisualization [
school book axes,
all axes={
grid={
major={style={red!50!black, opacity=.25}},
minor={style={green!25!black, opacity=.25}},
minor steps between steps=3,
}},
x axis={
label=$\phi$,
ticks and grid={
stack,
step=(2*pi),
tick typesetter/.code=\mytypesetter{##1},
}},
y axis={
label=$v$,
grid={step=1}
},
style sheet=vary hue,
visualize as line/.list={sin1}
]
data [set=sin1, format=function] {
var x : interval [-2*pi:2*pi] samples 100;
func y = sin(\value x r);
};
\end{tikzpicture}
\end{document}

• Change the interval [-2*pi:2*pi] to whatever you prefer ? – cfr Feb 6 '16 at 0:47
• Or do you mean that you don't really want to plot that function but a different one? – cfr Feb 6 '16 at 0:50

I am not very familiar with the datavisualization of tikz. So I recreated your picture with pgfplots.

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
height=6cm,
width=13cm,
axis lines=middle,
grid=both,
domain={-360:360},
ymin=-1.3, ymax=1.3,
xmin=-400, xmax=400,
major tick length=1ex,
minor tick length=0pt,
tick style={color=black,thin},
xtick={-360, 360},
xticklabels={$-2\pi$, $2\pi$},
minor xtick={-360,-270,...,360},
xlabel=$\phi$,
every axis x label/.style={
at={(ticklabel* cs:1)},
anchor=west,
},
xticklabel shift={.2cm},
ytick={-1,1},
yticklabels={$-1$, $1$},
minor ytick={-0.5,0.5},
ylabel=$v$,
every axis y label/.style={
at={(ticklabel* cs:1)},
anchor=south,
},
]
\addplot[thin, dashed] coordinates { (80, 0) (80, 0.642788) (0, 0.642788) };
\addplot[thick, black, samples=100] { sin(0.5*x) };
\addplot+[mark=*, color=black, mark options={scale=0.75,fill=black}] coordinates { (80, 0.642788) };
\node at (axis cs:0,0.642788) [anchor=east] {$\sigma_v$};
\node at (axis cs:80,0) [anchor=north] {$\sigma_\phi$};
\end{axis}
\end{tikzpicture}
\end{document}


Output:

• Thanks for your solution. How do you go about adding the gridlines like that found in the original code? Thanks. – Joe Feb 6 '16 at 6:13
• @Joe I added the grid. – Benjamin Feb 6 '16 at 7:18

Do you want this?

That's not sin x, though.

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{datavisualization.formats.functions, arrows}

\def\mytypesetter#1{
\pgfkeys{/pgf/number format/precision=2}
\pgfmathparse{#1/pi}
\pgfmathroundtozerofill{\pgfmathresult}
\pgfmathifisint{\pgfmathresult}{
\pgfmathprintnumber{\pgfmathresult}$\pi$%}
}{
\pgfmathprintnumber[/pgf/number format/frac, frac whole=false]{\pgfmathresult}$\pi$
}
}

\begin{document}
\begin{tikzpicture}
\datavisualization [
school book axes,
all axes={
grid={
major={style={red!50!black, opacity=.25}},
minor={style={green!25!black, opacity=.25}},
minor steps between steps=3,
}},
x axis={
label=$\phi$,
ticks and grid={
stack,
step=(2*pi),
tick typesetter/.code=\mytypesetter{##1},
}},
y axis={
label=$v$,
grid={step=1}
},
style sheet=vary hue,
visualize as line/.list={sin1}
]
data [set=sin1, format=function] {
var x : interval [-2*pi:2*pi] samples 100;
func y = sin(\value x/2 r);
};
\end{tikzpicture}
\end{document}

• thanks for your solution. How do I go about putting in the lines for \sigma v, and \sigma \phi? Thanks again. – Joe Feb 6 '16 at 1:57
• You could just use the ready-made pgfplots solution offered in the other answer. Or I guess it must be possible to annotate this. Do you need the datavisualization solution? – cfr Feb 6 '16 at 2:46
• with the pgfplots, how can you add the grid lines in the background? – Joe Feb 6 '16 at 6:11