I want to plot a Bode diagram and show the phase margin using tikz intersections.
Here is what I have done:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[european]{circuitikz}
\usepackage{tikz}
\usetikzlibrary{shapes, calc, intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{pgfplots.groupplots}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{subcaption}
\renewcommand*{\j}{\mathrm{j}}
\begin{document}
\begin{figure}[h]
\begin{tikzpicture}[trim axis right, remember picture]
\begin{semilogxaxis}[
name=mag,
width=\linewidth,
height=0.5\linewidth,
ylabel=$20\log_{10}\left(|H\!(\j2\pi f)\right|)$,
%xlabel=$f\,/\,\si{\Hz}$,
xmin=1000, xmax=1e10,
grid=both, minor grid style=dotted, minor y tick num = 4,
xticklabels={}]
\addplot [thick, name path=mag] table[x=Magnitude X, y=Magnitude Y, col sep=comma] {bode.csv};
\draw[red, thick, name path=0dB] (axis cs:1e3,0) -- (axis cs:1e10,0);
\end{semilogxaxis}
\begin{semilogxaxis}[
name=phase,
at={($(mag.south) - (0,0.05\linewidth)$)},
anchor=north,
width=\linewidth,
height=0.5\linewidth,
xmin=1000, xmax=1e10,
xlabel=$f\,/\,\si{\Hz}$,
ylabel=$\arg\{H(\j2\pi\omega)\}$,
grid=both, minor grid style=dotted, minor y tick num = 4]
\addplot [thick, name path= phase] table[x=Phase X, y=Phase Y, col sep=comma] {bode.csv};
\draw[red, thick, name path= PM] (axis cs:1e3,0) -- (axis cs: 1e10,0) coordinate (pmend);
\end{semilogxaxis}
\path [draw, name intersections={of=mag and 0dB,by={intmag}}, name path=vertical] (intmag) coordinate (IM) -- (IM|-phase.south);
\draw [latex-latex, name intersections={of=vertical and PM, by={intpm}}] (intpm) -- (intpm|-pmend) node[midway, left] {$PM$};
\end{tikzpicture}
\caption{Closed-loop Bode plot without Miller compensation}
\end{figure}
\end{document}
This is my result.
Which is wrong. It should look something similar to the pink lines in this picture.
I think the issue with my code has to do with the coordinate system being used outside and inside the axis environment which causes the result to be wrong.
How can I fix it?
Thanks
PD: csv file here

pdflatexinstead? If not, the other thing you can try to do is to use your original code and use the way to extract the coordinates from my answer? I switched to group plots because it is more elegant and because of the problems with shift reported here. (I do not fully agree with the answer to this question, though.) – Dec 18 '18 at 02:42\path [name intersections={of=mag and 0dB,by={intmag}}] (intmag) \pgfextra{\pgfplotspointgetcoordinates{\pgfpointanchor{intmag}{center}} \xdef\myx{\pgfkeysvalueof{/data point/x}}\typeout{\myx}};– jagjordi Dec 18 '18 at 11:08/data point/xrequires version 1.16, at least this is how I read this post. Which version do you have on overleaf?) – Dec 18 '18 at 22:58