tikzpicture 并排绘图

tikzpicture 并排绘图

是的,我看过其他解决方案,由于某种原因,它们不起作用。它们生成一组不居中的图像,如下图所示。 不居中

目前,我有以下代码:

\documentclass[12pt, letterpaper]{article}
\usepackage{amsmath}
\usepackage{hyperref} % add hyperlinks to refs
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz, pgfplots} 
\pgfplotsset{compat=newest}
\usepackage[export]{adjustbox} % centering floats
\usepackage{float} % H

\begin{document}
\begin{figure}[H]
  \centering
  \begin{subfigure}[b]{0.4\textwidth}
    \centering
    \begin{tikzpicture}
      \begin{axis}[
        axis lines=middle,
        xlabel={$n$},
        ylabel={$\boldsymbol{x[n]}$},
         xtick={-2, -1, ..., 2},
         width  = 0.4 * 8.5in,
        ]
        \addplot [ycomb, black, thick, mark=*] coordinates {
          (-1, -1)
          (0, 1)
          (1, -1)
          (2, 2)
        };
      \end{axis}
    \end{tikzpicture}
    \caption{x[n] Base signal;}
  \end{subfigure}%
  \hfil
  % add desired spacing between images, e. g. ~, \quad, \qquad etc.
  % (or a blank line to force the subfigure onto a new line)
  \begin{subfigure}[b]{0.4\textwidth}
    \centering
    \begin{tikzpicture}
      \begin{axis}[
        axis lines=middle,
        xlabel={$n$},
        ylabel={$\boldsymbol{x[n]}$},
         xtick={-2, -1, ..., 2},
         width  = 0.4 * 8.5in,
        ]
        \addplot [ycomb, black, thick, mark=*] coordinates {
          (0, -1)
          (1, 1)
          (2, -1)
          (3, 2)
        };
      \end{axis}
    \end{tikzpicture}
    \caption{x[n-1] shifted }
  \end{subfigure}
\end{figure}
\end{document}

我认为问题与日志输出有关,Overfull \hbox (63.08745pt too wide) in paragraph at lines 34--35但我不确定如何解决这个问题,有什么想法吗?

答案1

您确定的图表宽度太大。请尝试以下操作:

\documentclass[12pt, letterpaper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{subcaption}
\usepackage{amsmath}

\begin{document}
\begin{figure}[htb]
\pgfplotsset{width=\linewidth,% <--- accomodate diagram width to subfigure width
             axis lines=center,
             axis on top,
             xmin=-1.2, xmax=2.5,
             ymin=-1.2, ymax=2.5,
             x tick label style={font=\footnotesize, fill=white, yshift=-2pt},
             xlabel={$n$},
             x label style={anchor=west},
             ylabel={$\boldsymbol{x[n]}$},
             y label style={anchor=west},
             every axis plot post/.append style={ultra thick, black},
             },
 \centering
 \begin{subfigure}[b]{0.5\textwidth}
    \centering
    \begin{tikzpicture}
      \begin{axis}
        \addplot +[ycomb] coordinates { (-1, -1) (0, 1) (1, -1) (2, 2) };
      \end{axis}
    \end{tikzpicture}
    \caption{$x[n]$ Base signal}
  \end{subfigure}%
  \hfil
  \begin{subfigure}[b]{0.5\textwidth}
    \centering
    \begin{tikzpicture}
      \begin{axis}
        \addplot +[ycomb] coordinates { (0, -1) (1, 1) (2, -1) (3, 2) };
      \end{axis}
    \end{tikzpicture}
    \caption{$x[n-1]$ shifted }
  \end{subfigure}
\end{figure}
\end{document}

在此处输入图片描述

在上面的代码中,我擅自对图表代码进行了以下更改:

  • 我将轴属性的定义移至\pgfplotsset此图像的公共部分(这样图形的代码就更短了)
  • 在图表选项中我添加了xminxmaxyminymax通过它们我稍微增加了轴(对我来说这样看起来更好,如果你不喜欢这样,只需删除这些定义),
  • 更改xtick标签ytick尺寸,xtick label同时添加fill=white现在更清晰可见的
  • 移动顶部的轴
  • 定义绘制线条样式pgfplotset
  • \addplot添加选项+(as \addplot +[ycomb] ...

相关内容