Plotting sine function on a diagram

You do not necessarily need pgfplots for that. EDIT: Error fixed, big thanks to Max!

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\definecolor{mygray}{RGB}{127,127,127}
\newcommand{\vandbarrier}[1]{%
   \node at (#1, 1) {\scriptsize $V = \infty$};
   \node[scale = 0.5] at (#1, 0.5) {\textbf{(Barrier)}};%
}

\newcommand{\vabove}[1]{%
   \node[anchor = south] at (#1, 2) {\scriptsize $V = \infty$};%
}

\begin{tikzpicture}
\fill[mygray] % mygray is custom defined color.
    (0, 0) rectangle (2, 4)
    (4, 0) rectangle (6, 4);

\vandbarrier{1.0}
\vandbarrier{5.0}
\node[anchor = north] at (2, 0) {\scriptsize 0};
\node[anchor = north] at (4, 0) {\scriptsize $L$};
\node[anchor = north] at (6, 0) {\scriptsize $x$};

\draw[latex-latex,very thick] (0, 0) to (6, 0);
\draw[-latex,very thick] (2, 0) to (2, 4);
\draw[-latex,very thick] (4, 0) to (4, 4);

\draw[blue,thick] plot[variable=\x,domain=-1:1,smooth] ({\x+3},{1+0.4*cos(\x*120)});
\draw[blue,thick] plot[variable=\x,domain=-1:1,smooth] ({\x+3},{2-0.4*sin(\x*180)});
\draw[blue,thick] plot[variable=\x,domain=-1:1,smooth] ({\x+3},{3-0.4*cos(\x*270)});
\end{tikzpicture} 
\end{document}

enter image description here


An alternative to @marmot's answer which uses the sin and cos path commands:

enter image description here

\documentclass[tikz,margin=2mm]{standalone}

\definecolor{mygray}{gray}{0.4}

\begin{document}
% Diagram of a particle in 1D infinite potential well.
\newcommand{\vandbarrier}[1]{%
   \node at (#1, 1) {\scriptsize $V = \infty$};
   \node[scale = 0.5] at (#1, 0.5) {\textbf{(Barrier)}};%
}

\newcommand{\vabove}[1]{%
   \node[anchor = south] at (#1, 2) {\scriptsize $V = \infty$};%
}

\begin{tikzpicture}
\fill[mygray] % mygray is custom defined color.
    (0, 0) rectangle (2, 4)
    (4, 0) rectangle (6, 4);

\vandbarrier{1.0}
\vandbarrier{5.0}
\node[anchor = north] at (2, 0) {\scriptsize 0};
\node[anchor = north] at (4, 0) {\scriptsize $L$};
\node[anchor = north] at (6, 0) {\scriptsize $x$};

\draw[<->] (0, 0) to (6, 0);
\draw[->] (2, 0) to (2, 4);
\draw[->] (4, 0) to (4, 4);

\pgfmathsetmacro\amplitude{0.5}

\draw (2,1) sin ++(1,\amplitude) cos ++(1,-\amplitude);
\draw (2,2) sin ++(1/2,\amplitude) cos ++(1/2,-\amplitude) sin ++(1/2,-\amplitude) cos ++(1/2,\amplitude);
\draw (2,3) sin ++(1/3,\amplitude) cos ++(1/3,-\amplitude) sin ++(1/3,-\amplitude) cos ++(1/3,\amplitude) sin ++(1/3,\amplitude) cos ++(1/3,-\amplitude);

\end{tikzpicture} 
\end{document}

Edit
Just for fun, the same solution but now using a nested \foreach to draw an arbitrary number of waves with increasing frequency:

enter image description here

\documentclass[tikz,margin=2mm]{standalone}

\definecolor{mygray}{gray}{0.4}

\begin{document}
% Diagram of a particle in 1D infinite potential well.
\newcommand{\vandbarrier}[1]{%
   \node at (#1, 1) {\scriptsize $V = \infty$};
   \node[scale = 0.5] at (#1, 0.5) {\textbf{(Barrier)}};%
}

\newcommand{\vabove}[1]{%
   \node[anchor = south] at (#1, 2) {\scriptsize $V = \infty$};%
}

\begin{tikzpicture}
\fill[mygray] % mygray is custom defined color.
    (0, 0) rectangle (2, 4)
    (4, 0) rectangle (6, 4);

\vandbarrier{1.0}
\vandbarrier{5.0}
\node[anchor = north] at (2, 0) {\scriptsize 0};
\node[anchor = north] at (4, 0) {\scriptsize $L$};
\node[anchor = north] at (6, 0) {\scriptsize $x$};

\draw[<->] (0, 0) to (6, 0);
\draw[->] (2, 0) to (2, 4);
\draw[->] (4, 0) to (4, 4);

\pgfmathsetmacro\amplitude{0.2}

\foreach \i in {1,...,8}{
    \draw[blue] (2,{(\i-1)*\amplitude*2.2+1.1*\amplitude}) foreach \j [evaluate=\j as \dir using {(-1)^\j}]in {1,...,\i}{
        sin ++(1/\i,-1*\dir*\amplitude) cos ++(1/\i,\dir*\amplitude)
    };
}
%\draw (2,1) sin ++(1/1,\amplitude) cos ++(1/1,-\amplitude);
%\draw (2,2) sin ++(1/2,\amplitude) cos ++(1/2,-\amplitude) sin ++(1/2,-\amplitude) cos ++(1/2,\amplitude);
%\draw (2,3) sin ++(1/3,\amplitude) cos ++(1/3,-\amplitude) sin ++(1/3,-\amplitude) cos ++(1/3,\amplitude) sin ++(1/3,\amplitude) cos ++(1/3,-\amplitude);

\end{tikzpicture} 
\end{document}

An option result like the first given image, and some of the nice summary of the code shown in other answers.

RESULT:

enter image description here

MWE:

\documentclass[tikz,border=0pt]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage[scaled]{helvet}
\usepackage{stix}
\begin{document}
\definecolor{bg}{HTML}{FFE4C7}
\pagecolor{bg}
    \begin{tikzpicture}[
        %Environment cfg
        >={Stealth[length=9pt,inset=0]},
        font=\sffamily
    ]
    \def\L{3}
    \def\Deep{6}
    \def\Border{0.8}
    \def\h1{0.6}
    \def\Amp{1}
    \def\Spread{1.2}

    \fill[black!40]
    (0,0)
        |- ++(\L,-\Deep)
        |- ++(\Border,\Deep)
        |- ++(-\L -2*\Border,-\Deep -\Border)
        |- cycle;
    \draw[line width=2pt, <->]
    (0,0)++(0,\h1) 
        node[anchor=south](T1){$\mathsf{\infty}$}
        |- ++(\L,-\Deep-\h1) -- ++(0,\Deep+\h1)
        node[anchor=south](T2){$\mathsf{\infty}$};
    \draw[thick,<->]
        (0,-\Deep+0.5) -- ++ (\L,0) node[midway,fill=bg]{L = \L};

    \foreach \n in {1,2,3,4}{
    \draw[very thick]
        (-0.4,-\Deep+\n*\Spread) node{n=\n}
        plot[
            variable=\x,
            domain=0:\L,
            smooth
        ]({\x},{-\Deep+\n*\Spread+0.4*sin(\x*360*0.5*\n/\L)});
    }
    \end{tikzpicture} 
\end{document}

Tags:

Tikz Pgf