I need help to replicate the following figure in TikZ, which schematically describes the difference between (a) a transverse and (b) a longitudinal wave travelling through a spring. I tried finding a mathematical function to describe the vibrating spring, which is quite simple in the transverse case but I can't find a suitable function for the longitudinal one. Any clues? May be it's better to use pathmorphing?

Thanks in advance! Regards.

enter image description here

Here's the code I'm working with, as a MWE:


    axis y line=middle,
    axis x line=middle,
    x label style={at={(axis description cs:0.875,0.595)},anchor=east},
    y label style={at={(axis description cs:0.08,1.4)},anchor=north},
    no markers,
    every axis plot/.append style={thick}
    \addplot[blue,thick,samples=400,domain=0:10.5] (\x,
    \draw[latex-latex,line width=3pt,purple] (-0.5,-0.8) -- (-0.5,0.8);
    \draw[densely dashed] (1.57,1.5) -- (1.57,2);
    \draw[densely dashed] (7.85,1.5) -- (7.85,2);
    \draw[latex-latex] (1.57,1.8) -- (7.85,1.8) node[midway,above] {$\lambda$};
    \draw[-latex,thick] (1.07,-0.75) -- (2.07,-0.75) node[midway,above] {$v$};
    \node at (-0.5,5) {(a)};

        % the second graph here

closed as too broad by Zarko, Stefan Pinnow, Mensch, TeXnician, Phelype Oleinik Feb 22 '18 at 19:26

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You would making the task much simpler if you provided the formulae for the two plots. For instance, I have problems seeing whether or not the frequency changes in the upper plot. And the lower wave seems to have three different frequencies, low - medium - high - medium - low - medium - high - medium - low. – user121799 Feb 22 '18 at 18:09
  • 1
    first image seems to be sum of two sinusoids, the second one is chirp. i see question more math related than to latex. in latex is "do.this-to-me" of sort . – Zarko Feb 22 '18 at 18:17

Assuming that your question is how one may plot a wave with varying frequency, here is a proposal. The idea is to increase the "speed" in x-direction along the plot. In this MWE, this is achieved by adding some Gaussians to the x coordinate.

\tikzset{declare function={f(\x)=sin(540*\x);}}
 \draw[thick,-latex] (0,-2) -- (0,5)node[right] {$\varphi$};
 \draw[thick,-latex] (-1,0) -- (10,0)node[below] {$x$};
 \draw[domain=0.1:9.5,variable=\x,samples=500] plot
 \draw[latex-latex] (1,2) -- (7,2) node[midway,above]{$\lambda$};

enter image description here

I deliberately kept the example minimal, but clearly you can plot the same thing with pgfplots, and I can see that you have no problem with plotting things using pgfplots.

EDIT: Increased the sampling, thanks to Christian Hupfer!

  • Perhaps the sampling should be increased here. The peaks look very sharp – user31729 Feb 22 '18 at 19:15
  • Many thanks! This is what I needed, indeed! I apologize if I didn't post a "well asked" question. – Nicolás Budini Feb 24 '18 at 1:12

Yes (agreeing with marmot), you should probably use plots. See this example. The relevant line/command would be this one:

    \draw[smooth,samples=200,color=blue] plot function{(\cA)* (cos((\cC)*x+(\cD))) + \cB} 
    node[right] {$f(x) = \cA{} . cos(\cC{} . x + \cD{}) + \cB{}$};

EDIT: Probably better example with pgfplots

This looks like a better example. It has \usepackage{pgfplots}. The relevant lines:


I think my first suggestions needs external programs (GNU plot) and a bit of hacking, and hopefully the second one does not.


Change the title of your question (if possible) to something more descriptive than "this figure", e.g. "a figure about frequencies" or something like that.

Not the answer you're looking for? Browse other questions tagged or ask your own question.