我迫切地想将一个方程图(由一堆正弦波组成)与位于其右侧的代表其傅里叶变换的 ybar 图对齐。我使用了 subfloat 环境来获得两个不同的图形,然后我可以分别引用和添加标题。很快出现的问题是,我似乎无法弄清楚如何正确对齐这两个图形,我相信问题出在 ybar 图的结构和布局与普通图形不同。我尝试隐藏条形图的刻度标记,但这并没有解决问题。我该如何解决这个问题?提前致谢!
\begin{figure}[H]
\centering
\subfloat[][]{
\begin{tikzpicture}
\begin{axis}[
ymajorgrids=true,
grid style=dashed,tick pos=left,width=9cm,height=8cm]
\addplot[domain=0:1, samples=200, red, very thin]{6*sin(4*pi*deg(x))};
\addplot[domain=0:1, samples=200, cyan, very thin]{4*sin(8*pi*deg(x))};
\addplot[domain=0:1, samples=200, orange, very thin]{3*sin(12*pi*deg(x))};
\addplot[domain=0:1, samples=200, green, very thin]{2*sin(16*pi*deg(x))};
\addplot[domain=0:1,samples=200,blue, ultra thick]{6*sin(4*pi*deg(x))+4*sin(8*pi*deg(x))+3*sin(12*pi*deg(x))+2*sin(16*pi*deg(x))};
\end{axis}
\end{tikzpicture}
}
\subfloat[][]{
\begin{tikzpicture}
\begin{axis}[ybar,xmin=0,xmax=1000,tick pos=left, width=9cm,height=8cm]
\addplot
coordinates{(200,6) (400,4) (600,3) (800,2)};
\end{axis}
\end{tikzpicture}
}
\end{figure}
答案1
这是一种使用包的方法floatrow
:
\documentclass{article}
\usepackage[utf8]{inputenc} %
\usepackage{geometry}
\usepackage{caption, subcaption, floatrow, tikz, pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}[!ht]
\ffigbox{%
\floatsetup{style=plain, heightadjust=object, valign=t}
\begin{subfloatrow}
\ffigbox[\FBwidth]{\caption{}\label{fig:1}}
{\begin{tikzpicture}
\begin{axis}[
ymajorgrids=true,
grid style=dashed,tick pos=left,width=9cm,height=8cm]
\addplot[domain=0:1, samples=200, red, very thin]{6*sin(4*pi*deg(x))};
\addplot[domain=0:1, samples=200, cyan, very thin]{4*sin(8*pi*deg(x))};
\addplot[domain=0:1, samples=200, orange, very thin]{3*sin(12*pi*deg(x))};
\addplot[domain=0:1, samples=200, green, very thin]{2*sin(16*pi*deg(x))};
\addplot[domain=0:1,samples=200,blue, ultra thick]{6*sin(4*pi*deg(x))+4*sin(8*pi*deg(x))+3*sin(12*pi*deg(x))+2*sin(16*pi*deg(x))};
\end{axis}
\end{tikzpicture}}
\ffigbox[\FBwidth]{\caption{}\label{fig:2}}
{\begin{tikzpicture}
\begin{axis}[ybar,xmin=0,xmax=1000,tick pos=left, width=9cm,height=8cm]
\addplot
coordinates{(200,6) (400,4) (600,3) (800,2)};
\end{axis}
\end{tikzpicture}}
\end{subfloatrow}}
{\caption{Sine waves and their Fourier transforms}\label{fig}}
\end{figure}
\end{document}