3

I am trying to plot the graph shown below (even arrows or lines to show important frequency points would be enough) I have been searching for hours, but I cannot find a sample code that can help me plot it. Could you help me?

enter image description here

  • 1
    I recommend not plotting directly in LaTeX, but in Python using matplotlib, which can be configured to output PGF-code (which you can use in LaTeX with \usepackage{pgf}). – Skillmon Nov 10 '19 at 17:00
  • 2
    I do recommend plotting directly in LaTeX (using tikz/pgfplots/metapost). What, exactly, is it that you have a problem to plot here? Do you have your data? Do you know for what values you want those dotted lines? Your question will be better if you show some code of what you are trying and which parts you have problems with. – mickep Nov 10 '19 at 19:31
  • @mickep why would you plot this in LaTeX? The number crunching in LaTeX is slow, the data is plotted in a semi-logarithmic scale, do you recommend calculating all those logarithms in LaTeX? With matplotlib you can create your plots in a way that they fit perfectly into your document and it'll be way faster (and the rendering will still be done through LaTeX, so you get all the typographic features LaTeX has to offer). I honestly don't see the advantage of LaTeX here. – Skillmon Nov 10 '19 at 20:25
  • 1
    @Skillmon Sorry, my comment might have been interpreted as a bit rough against your comment. That was not my purpose. I just think that the tools to do plots fromwithin TeX are very convenient and powerful, and if one make them in the .tex file, it is easy to have a coherent style. As we see in the answer given, it seems to work also well in this case. But of course I have nothing against matplotlib or other tools (I use Mathematica heavily myself). – mickep Nov 11 '19 at 19:56
  • 1
    @mickep I agree with you, This is a LaTeX site. I personally also use Mathematica for more involved plots. But as you say, one needs to gauge whether fiddling with Mathematica long enough to have a coherent output for all plots, or having more pain when coding the plot and having to wait a bit longer for the compilation is the "greater evil". The answer has to be given case by case IMHO. –  Nov 11 '19 at 21:31
  • 1
    @mickep in retro-spective my second comment seems a bit harsh. Sorry, I'm just so used to combine matplotlib with LaTeX, – Skillmon Nov 11 '19 at 22:25

1 Answers1

4

Welcome! If you want to plot real data, you need real data. This answer assumes you wish to produce a cartoon of the depicted plot. This can be done rather easily with pgfplots, as advertised in the comments. One may define a fake peak function (which is actually not too much of a fake because this is the Breit-Wigner form of a peak), which depends on the plot variable x, and has two more parameters controlling the location and the width. If you add several of those (with prefactors fixing the heights of the respective peaks), you get

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=12cm,compat=1.16}% <- if you have an older installation, try 1.15 or 1.14
\begin{document}
\begin{tikzpicture}[declare function={%
    peak(\x,\u,\v)=ifthenelse(\x>\u*(1-\v) && \x<\u*(1+\v),
    -\v/(1+\v)+\v/(\v+pow((\x-\u)/(\u*\v),2)),0);}]
\begin{axis}[xmode=log,
  xminorgrids,
  ymajorgrids,
  grid style={densely dashed,thin},
  xtick={1,10,100,1000,10000,100000},
  xticklabels={$1\mathsf{kHz}$,$10\mathsf{kHz}$,$100\mathsf{kHz}$,
    $1\mathsf{MHz}$,$10\mathsf{MHz}$},
  yticklabel=$\mathsf{\pgfmathprintnumber{\tick}}\mathsf{A}$,
  ytick={0,0.8,...,8.8},
  ymax=8.1]
 \addplot[blue,no marks,domain=0.5:30000,samples=501] 
 {7.9*peak(x,130,0.1)+0.3*peak(x,170,0.2)+0.8*peak(x,220,0.1)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

If you add more peaks, you can qualitatively reproduce your screen shot. As for the timing: on a five year old MacBook pro this takes 5 seconds to compile. You could speed it up by using gnuplot but personally I find 5 seconds not too bad. There are other things you may want to add like siunits for the units and so on, but I prefer to keep this code here rather minimal.