4

I have an issue I trust you will solve.

I am plotting two graphs in order to represent Bode diagrams. For correspondance of abscissa they should be one above the other. I can align the figures but because the value on the y axis may differ, the plots themselves may not be aligned.

See for yourselves:

Unaligned plots

Can you help me ?

Plus, this is just in "standalone" document, so I used "varwidth" to have them on top of each other but how will I achieve the same result inside an "article" document ?

Created with this code :

\documentclass[varwidth]{standalone}
\usepackage{pgfplots}

\begin{document}

\def\T{10}
\def\K{1000}
\def\FloorW{floor(ln(1/\T)/ln(10))}
\def\CeilW{ceil(ln(1/\T)/ln(10))}

\begin{tikzpicture}
\begin{semilogxaxis}[height=5cm,width=10cm,
grid=both, tick align=outside, tickpos=left]

\def\GdbK{20*ln(\K)/ln(10)}

\addplot [domain=(10^(\FloorW-2)):(1/\T),samples=2] {\GdbK}[red];
\addplot [domain=(1/\T):(10^(\CeilW+2)),samples=2] {\GdbK-(10*(ln(\T^2*x^2)))/ln(10)}[red]; 

\end{semilogxaxis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{semilogxaxis}[height=5cm,width=10cm,
grid=both, tick align=outside, tickpos=left,
ytick=\empty,extra y ticks={0,-45,-90} ]

\addplot [mark=none] coordinates
{(10^(\FloorW-2),0) (1/\T,0) (1/\T,-90) ((10^(\CeilW+2),-90)}[red];

\end{semilogxaxis}
\end{tikzpicture}
\end{document}
6

Just put both graphics in the same tikzpicture environment and move the second one down with yshift=-4.5cm

\documentclass[varwidth]{standalone}
\usepackage{pgfplots}

\begin{document}

\def\T{10}
\def\K{1000}
\def\FloorW{floor(ln(1/\T)/ln(10))}
\def\CeilW{ceil(ln(1/\T)/ln(10))}

\begin{tikzpicture}
\begin{semilogxaxis}[height=5cm,width=10cm,
grid=both, tick align=outside, tickpos=left]

\def\GdbK{20*ln(\K)/ln(10)}

\addplot [domain=(10^(\FloorW-2)):(1/\T),samples=2] {\GdbK}[red];
\addplot [domain=(1/\T):(10^(\CeilW+2)),samples=2] {\GdbK-(10*(ln(\T^2*x^2)))/ln(10)}[red]; 

\end{semilogxaxis}
%\end{tikzpicture}
%
%\begin{tikzpicture}
\begin{semilogxaxis}[yshift=-4.5cm,height=5cm,width=10cm,
grid=both, tick align=outside, tickpos=left,
ytick=\empty,extra y ticks={0,-45,-90} ]

\addplot [mark=none] coordinates
{(10^(\FloorW-2),0) (1/\T,0) (1/\T,-90) ((10^(\CeilW+2),-90)}[red];

\end{semilogxaxis}
\end{tikzpicture}
\end{document}

screenshot

  • Thanks, that's exactly what I was looking for. Quick follow up question, how come -4.5cm is enough when the height of the first plot is set to 5cm ? – LMT-PhD Feb 17 at 13:22
  • This is because on the y-axis, it displays the graduations between 20 and 60 and not those between 20 and 0. – AndréC Feb 17 at 14:46
  • I don't know, I changed \K to 10, to change the values between -20 and 20 and it doesn't affect the display positions. – LMT-PhD Feb 17 at 14:49
  • The part displayed is the useful part, if I may say so. Since there is no data between 0 and 20, by default the graph is reduced to useful dimensions – AndréC Feb 17 at 14:52
3

off-topic since your problem is solved by @AndréC's answer. i would write his mwe on the following way:

\documentclass[varwidth, margin=3mm]{standalone}
\usepackage{pgfplots}

\begin{document}

\def\T{10}
\def\K{1000}
\def\FloorW{floor(ln(1/\T)/ln(10))}
\def\CeilW{ceil(ln(1/\T)/ln(10))}

    \begin{tikzpicture}
\pgfplotsset{height=5cm,width=10cm,
             grid=both,
             %tick align=outside,
             tickpos=left,
             no marks}
\begin{semilogxaxis}
\def\GdbK{20*ln(\K)/ln(10)}

\addplot [red,domain=(10^(\FloorW-2)):(1/\T),samples=2] {\GdbK};
\addplot [red,domain=(1/\T):(10^(\CeilW+2)),samples=2] {\GdbK-(10*(ln(\T^2*x^2)))/ln(10)};
\end{semilogxaxis}
%
\begin{semilogxaxis}[yshift=-44mm,
                     ytick={0,-45,-90}]
\addplot [red] coordinates
{(10^(\FloorW-2),0) (1/\T,0) (1/\T,-90) (10^(\CeilW+2),-90)};
\end{semilogxaxis}
    \end{tikzpicture}
\end{document}

result is the same as at @AndréC's answer.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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