如何使用图形将两个 tikz 图片水平放置并排放置

如何使用图形将两个 tikz 图片水平放置并排放置
 \begin{figure}
  \begin{tikzpicture}
\begin{axis}[
    xmin=-5,    xmax=5, xlabel=$t$, 
    ymin=-2,    ymax=2, ylabel=$x(t)$,
    grid=major, 
%
    domain=-5:5,
    samples=125,
    every axis plot post/.append style={line width=1pt}    
            ]
\addplot [red]  expression{sin(90*x)+0.5};
\addplot [blue] expression{sin(90*x)+0.4*rand};
\addplot [red]  expression{sin(90*x)-0.5};
\end{axis}
    \end{tikzpicture}
    \caption{Exp1}
 \label{Slicica:fig2}


\begin{tikzpicture} 
        \begin{axis}[
            xlabel=\(x\),
            ylabel=\(y\),
            xmin=-7,
            xmax=7,
            ymin=-30,
            ymax=30,
            grid=major,
            grid style={solid},
            samples=100
        ]
            \foreach \a in {-2.4, -2.1, ..., 2.4} {
                \addplot[
                    domain=-7:7, 
                    red, 
                    dashed
                ] expression{2*\a*x^2 + 12*x};
                \addplot[
                    domain=-7:7,
                    cyan
                ] 
                expression{2*\a*x^2 - 12*x};
            }
        \end{axis}
    \end{tikzpicture}
\caption{Exp2}
 \label{Slicica:fig3}
\end{figure}

所以有人能帮助我如何使用 \figure 将这两张图片水平对齐吗?因为我需要为这些图片设置标题

答案1

我会做以下事情:

  • 如果这两个图属于同一主题,我会使用subfigure,请参见下面代码中的选项 1。
  • 如果这些图应该单独编号,也许multicols可以满足您的要求,请参阅选项 2。
\documentclass{article}
\usepackage[a4paper,left=.2cm,right=.2cm,top=2cm,bottom=2cm]{geometry} % just in order to have the figures not overlapping

\usepackage{tikz,pgfplots}
\usepackage{subcaption} % provides subfigure
\usepackage{multicol} % provides multicols

\begin{document}

%%%%% OPTION 1 WITH SUBFIGURE %%%%%

\begin{figure}
   \begin{subfigure}[b]{.5\textwidth}
      %\centering
      \begin{tikzpicture}
         \begin{axis}[
            xmin=-5,    xmax=5, xlabel=$t$, 
            ymin=-2,    ymax=2, ylabel=$x(t)$,
            grid=major, 
            %
            domain=-5:5,
            samples=125,
            every axis plot post/.append style={line width=1pt}    
            ]
            \addplot [red]  expression{sin(90*x)+0.5};
            \addplot [blue] expression{sin(90*x)+0.4*rand};
            \addplot [red]  expression{sin(90*x)-0.5};
         \end{axis}
      \end{tikzpicture}
      \caption{Exp1}
      \label{Slicica:fig2}
   \end{subfigure}
   \hfill
   \begin{subfigure}[b]{.5\textwidth}
      %\centering
      \begin{tikzpicture} 
         \begin{axis}[
            xlabel=\(x\),
            ylabel=\(y\),
            xmin=-7,
            xmax=7,
            ymin=-30,
            ymax=30,
            grid=major,
            grid style={solid},
            samples=100
            ]
            \foreach \a in {-2.4, -2.1, ..., 2.4} {
               \addplot[
               domain=-7:7, 
               red, 
               dashed
               ] expression{2*\a*x^2 + 12*x};
               \addplot[
               domain=-7:7,
               cyan
               ] 
               expression{2*\a*x^2 - 12*x};
            }
         \end{axis}
      \end{tikzpicture}
      \caption{Exp2}
      \label{Slicica:fig3}
   \end{subfigure}
   \caption{caption to both figures}
\end{figure}


%%%%% OPTION 2 WITH MULTICOLS %%%%%
 
\begin{multicols}{2}
   \begin{tikzpicture}
      \begin{axis}[
         xmin=-5,    xmax=5, xlabel=$t$, 
         ymin=-2,    ymax=2, ylabel=$x(t)$,
         grid=major, 
         %
         domain=-5:5,
         samples=125,
         every axis plot post/.append style={line width=1pt}    
         ]
         \addplot [red]  expression{sin(90*x)+0.5};
         \addplot [blue] expression{sin(90*x)+0.4*rand};
         \addplot [red]  expression{sin(90*x)-0.5};
      \end{axis}
   \end{tikzpicture}
   \captionof{figure}{Exp2}
   \label{Slicica:fig2_ALT}

   \begin{tikzpicture} 
      \begin{axis}[
         xlabel=\(x\),
         ylabel=\(y\),
         xmin=-7,
         xmax=7,
         ymin=-30,
         ymax=30,
         grid=major,
         grid style={solid},
         samples=100
         ]
         \foreach \a in {-2.4, -2.1, ..., 2.4} {
            \addplot[
            domain=-7:7, 
            red, 
            dashed
            ] expression{2*\a*x^2 + 12*x};
            \addplot[
            domain=-7:7,
            cyan
            ] 
            expression{2*\a*x^2 - 12*x};
         }
      \end{axis}
   \end{tikzpicture}
   \captionof{figure}{Exp2}
   \label{Slicica:fig3_ALT}
\end{multicols}

\end{document}

在此处输入图片描述

相关内容