1

I am having 2 figure with separated figure using tikzpicture

How can I make it into subfigure below:

enter image description here

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

My figure for tikzpicture are: enter image description here

\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} 
aan
  • 2,663

1 Answers1

2

The following MWE contains at first a really quick-and-dirty solution with \resizebox, which I used to enforce the desired width (indeed, the aspect ratio is not perfect). At second I've redone the plots using the axis environment provided by pgfplots, that lets you specify the width and height of a plot, and keep a perfect aspect ratio between the axes.

More on that here: Specifying the width and height of a 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}

enter image description here