將 tikz 圖片中的繪圖一層一層對齊

將 tikz 圖片中的繪圖一層一層對齊

我有一個問題相信你會解決。

我正在繪製兩個圖來表示波特圖。為了橫座標的對應,它們應該一個在另一個之上。我可以對齊數字,但由於 y 軸上的值可能不同,因此圖本身可能不會對齊。

自己看看:

未對齊的繪圖

你能幫助我嗎 ?

另外,這只是在“獨立”文檔中,所以我使用“varwidth”將它們放在彼此之上,但是如何在“文章”文檔中實現相同的結果?

使用此程式碼建立:

\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}

答案1

只需將兩個圖形放在同一tikzpicture環境中,然後將第二個圖形向下移動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}

螢幕截圖

答案2

題外話,因為你的問題已經被 @AndréC 的答案解決了。我會用以下方式寫下他的mwe:

\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}

結果與@AndréC 的答案相同。

相關內容