是否可以将 2 张 tikzpicture 组合成一个水平消失的图形?

是否可以将 2 张 tikzpicture 组合成一个水平消失的图形?

我有两个数字,使用分开的数字tikzpicture

我怎样才能将其变成下面的子图:

在此处输入图片描述

\begin{figure}[htp!]
        \begin{subfigure}{0.5\textwidth}
            \centering
            % include first image
            \includegraphics[width=1\linewidth]{A.png}  
    \caption{A}
    \label{A}
        \end{subfigure}
        \begin{subfigure}{.5\textwidth}
            \centering
            % include second image
            \includegraphics[width=\linewidth]{B.png} 
    \caption{Combine A and B}
        \end{subfigure}
    \caption{B}
    \label{B}
    \end{figure}

我对 tikzpicture 的印象如下: 在此处输入图片描述

\begin{figure}[h!]
\centering
  \begin{tikzpicture}[font=\sffamily,
    declare function={f1(\x)=1.2*sin(2.5*deg(\x));
    f2(\x)=0.6*(1.2*sin(2*deg(\x))+1*sin(4*deg(\x))+1.2*sin(6*deg(\x)));
    }]
 \begin{scope}[local bounding box=T]
   \draw[latex-latex](0,3)  node[above]{Amplitude} |- (8,0) ;
   \draw plot[domain=0:7,variable=\x,samples=51,smooth] ({\x},{f1(\x)});
  \end{scope}

 \path foreach \X in {T} {(\X.east) node[below] {Time}}; 
\end{tikzpicture}
    \caption{Simple pure tone (sine wave)}
    \label{fig:Simple pure tone (sine wave)}
\end{figure}  


\begin{figure}[h!]
\centering
  \begin{tikzpicture}[font=\sffamily,
    declare function={f1(\x)=1.2*sin(2.5*deg(\x));
    f2(\x)=0.6*(1.2*sin(2*deg(\x))+1*sin(4*deg(\x))+1.2*sin(6*deg(\x)));
    }]
  \begin{scope}[yshift=-6cm,local bounding box=B]
   \draw[latex-latex](0,3)  node[above]{Amplitude} |- (10,0);
   \draw plot[domain=0:9,variable=\x,samples=101,smooth] ({\x},{f2(\x)});
  \end{scope}
 %
 \path foreach \X in {B} {(\X.east) node[below] {Time}}; 
\end{tikzpicture}
    \caption{Complex waveform (sum of pure tone)}
    \label{fig:Complex waveform (sum of pure tone)}
\end{figure} 

答案1

以下 MWE 首先包含一个非常快速而粗糙的解决方案 \resizebox,我用它来强制执行所需的宽度(实际上,纵横比并不完美)。其次,我使用axis提供的环境重新绘制了图表pgfplots,该环境允许您指定图表的宽度和高度,并保持轴之间的完美纵横比。

更多详情请参见此处:指定 tikzpicture 的宽度和高度

\documentclass[]{article}

\usepackage{subcaption}
\usepackage{pgfplots}

\begin{document}

\begin{figure}
    \centering
    \hfill
    \subcaptionbox{%
        Simple pure tone (sine wave)
        \label{fig:Simple pure tone (sine wave)}
    }{%
        \resizebox{.45\textwidth}{.3\textwidth}{%
            \begin{tikzpicture}[
                    font=\sffamily,
                    declare function={%
                        f1(\x) = 1.2*sin(2.5*deg(\x)); f2(\x)=0.6*(1.2*sin(2*deg(\x ))+1*sin(4*deg(\x))+1.2*sin(6*deg(\x)));
                    }
                ]
             \begin{scope}[local bounding box=T]
               \draw[latex-latex](0,3)  node[above]{Amplitude} |- (8,0) ;
               \draw plot[domain=0:7,variable=\x,samples=51,smooth] ({\x},{f1(\x)});
              \end{scope}

             \path foreach \X in {T} {(\X.east) node[below] {Time}};
            \end{tikzpicture}
        }
    }
    \hfill
    \subcaptionbox{%
        Complex waveform (sum of pure tone)
        \label{fig:Complex waveform (sum of pure tone)}
    }{%
        \resizebox{.45\textwidth}{.3\textwidth}{%
            \begin{tikzpicture}[font=\sffamily,
                declare function={f1(\x)=1.2*sin(2.5*deg(\x));
                f2(\x)=0.6*(1.2*sin(2*deg(\x))+1*sin(4*deg(\x))+1.2*sin(6*deg(\x)));
                }]
              \begin{scope}[yshift=-6cm,local bounding box=B]
               \draw[latex-latex](0,3)  node[above]{Amplitude} |- (10,0);
               \draw plot[domain=0:9,variable=\x,samples=101,smooth] ({\x},{f2(\x)});
              \end{scope}
             %
             \path foreach \X in {B} {(\X.east) node[below] {Time}};
            \end{tikzpicture}
        }
    }
    \hspace*{\fill}
    \caption{B}
    \label{B}
\end{figure}

\begin{figure}
    \centering
    \hfill
    \subcaptionbox{%
        Simple pure tone (sine wave)
        \label{fig:Simple pure tone (sine wave)}
    }{%
        \begin{tikzpicture}
        \begin{axis}[
            width=.475\textwidth,
            axis lines = middle,
            enlargelimits=.05,
            xtick=\empty,
            ytick=\empty,
            font={\scriptsize\sffamily},
            ylabel = Amplitude,
            xlabel = Time,
            every axis x label/.style={%
                at={(ticklabel* cs:1.0)},
                anchor=west,
            },
            every axis y label/.style={%
                at={(ticklabel* cs:1.0)},
                anchor=south,
            },
            declare function={%
                f1(\x)=1.2*sin(2.5*deg(\x));
                f2(\x)=0.6*(1.2*sin(2*deg(\x))+1*sin(4*deg(\x))+1.2*sin(6*deg(\x)));
            }
        ]
        \addplot[domain=0:7,variable=\x,samples=51,smooth] ({\x},{f1(\x)});
        \end{axis}
        \end{tikzpicture}
    }
    \hfill
    \subcaptionbox{%
        Complex waveform (sum of pure tone)
        \label{fig:Complex waveform (sum of pure tone)}
    }{%
        \begin{tikzpicture}
        \begin{axis}[
            width=.475\textwidth,
            axis lines = middle,
            enlargelimits=.05,
            xtick=\empty,
            ytick=\empty,
            font={\scriptsize\sffamily},
            ylabel = {Amplitude},
            xlabel = {Time},
            every axis x label/.style={%
                at={(ticklabel* cs:1.0)},
                anchor=west,
            },
            every axis y label/.style={%
                at={(ticklabel* cs:1.0)},
                anchor=south,
            },
            declare function={%
                f1(\x)=1.2*sin(2.5*deg(\x));
                f2(\x)=0.6*(1.2*sin(2*deg(\x))+1*sin(4*deg(\x))+1.2*sin(6*deg(\x)));
            }
        ]
        \addplot[domain=0:9,variable=\x,samples=101,smooth] ({\x},{f2(\x)});
        \end{axis}
        \end{tikzpicture}
    }
    \hspace*{\fill}
    \caption{Same as \ref{B}, but with \texttt{pgfplots}.}
    \label{BB}
\end{figure}
\end{document}

在此处输入图片描述

相关内容