如何在多列中垂直和水平对齐多个 pgfplots?

如何在多列中垂直和水平对齐多个 pgfplots?

我刚开始使用 LaTeX,正在为我的学生创建一个包含多个图表的工作表。我习惯于multicol组织它们并枚举它们以对它们进行编号。我还设置了每个图的宽度和高度,使它们相同。然而,当我的代码编译时,图表在水平和垂直方向上略微不对齐。有什么简单的方法可以解决这个问题吗?谢谢

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{exercise}
\usepackage{amsmath}
\usepackage{systeme}

\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\usepackage{tikz}
\pgfplotsset{width=6cm, height=6cm, compat=1.18} 

\begin{document}
Write the equation of the lines in the following graphs.
    \begin{multicols}{3}
        \begin{enumerate}
             \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        xmin=-1,
                        xmax=6,
                        ymin=-8,
                        ymax=2,
                        %enlargelimits={abs=0.5},
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-1:4]{3*x-7};
                    \end{axis}
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        ymin=-1,
                        ymax=6,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-4:4]{2};
                    \end{axis}
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        ymin=-2,
                        ymax=6,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-1:8]{-(2/3)*x+4};
                    \end{axis}
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        ymin=-3,
                        ymax=7,
                        xmin=-4,
                        xmax=4,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-4:4]{(4/3)*x+2};
                    \end{axis}
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        xmin=-1,
                        xmax=5,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-1:8] coordinates{(3,-3) (3,3)};
                    \end{axis}
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        xmin=-4,
                        xmax=3,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-4:1]{-2*x-1};
                    \end{axis}
                \end{tikzpicture}
        \end{enumerate}
    \end{multicols}
\end{document}

答案1

如果您想要表格数据,请使用环境而不是和的tabular混合。multicolenumerate

我将所有图片的比例改为 0.8,以适应网格中的所有图形,并将字体大小保留为默认值。看起来还不错。我还应用了相同的轴范围。我个人认为它看起来更一致。但是,第一张和第三张图差别很大,应该有自己的设置。我还添加了trim axis left和,trim axis right以便将边界框设置为轴。当标签出现在图片边缘时,这可以防止图形向左或向右移动。

两个轴和每张图片的全局设置都移至前言以避免重复。出于同样的原因,我定义了一个新的数字计数器graph来为每个图形编号。以下行格式化计数器:

\renewcommand*{\thegraph}{\arabic{graph})}

1)例如,当前设置为。

这是解决方案

在此处输入图片描述

\documentclass{article}
\usepackage{array}
\usepackage{exercise}
\usepackage{amsmath}
\usepackage{systeme}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikz}

\usepackage{showframe}

%%% Global settings for tikz pictures
\tikzset{
  >=latex,
  baseline={([yshift=-2.5ex]current bounding box.north)},
  trim axis left, trim axis right,
}
\pgfplotsset{
  grid=both,
  axis lines=middle,
  minor tick num=1,
  axis line style={latex-latex},
  ytick distance=2,
  ticklabel style={fill=white},   % font=\normalsize
  xmin=-4, xmax=4, ymin=-2, ymax=6,
  width=5cm, height=5cm, compat=1.18,
}
\newcounter{graph}
\renewcommand*{\thegraph}{\arabic{graph})}   % Formats the counter


\begin{document}
Write the equation of the lines in the following graphs.

\begin{figure}[tbh]
  \setlength\tabcolsep{3pt} % space between a number and a graph
  \centering
  \begin{tabular}{@{}
      rc
      @{\hspace{2em}}   % Space between 1st and 2nd graph in a row
      rc
      @{\hspace{2em}}   % Space between 2st and 3rd graph in a row
      rc
    @{}}
    \stepcounter{graph}\thegraph &
        \begin{tikzpicture}[
          scale = 0.8,
        ]
          \begin{axis}[
            xmin=-2, xmax=6, ymin=-10, ymax=2,
          ]
            \addplot[line width=1.5pt, domain=-1:4]{3*x-7};
          \end{axis}
        \end{tikzpicture}
    &
    \stepcounter{graph}\thegraph &
      \begin{tikzpicture}[
          scale = 0.8,
        ]
          \begin{axis}
            \addplot[line width=1.5pt, domain=-4:4]{2};
          \end{axis}
        \end{tikzpicture}
    &
    \stepcounter{graph}\thegraph &
      \begin{tikzpicture}[
        scale = 0.8,
      ]
        \begin{axis}[
          xmin=-1, xmax=7,
        ]
          \addplot[line width=1.5pt, domain=-1:7]{-(2/3)*x+4};
        \end{axis}
      \end{tikzpicture}
    \\
    \rule[8pt]{0pt}{2em} % small strut
    \stepcounter{graph}\thegraph &
      \begin{tikzpicture}[
        scale = 0.8,
      ]
        \begin{axis}
          \addplot[line width=1.5pt, domain=-4:4]{(4/3)*x+2};
        \end{axis}
      \end{tikzpicture}
    &
    \stepcounter{graph}\thegraph &
      \begin{tikzpicture}[
        scale = 0.8,
      ]
        \begin{axis}
          \addplot[line width=1.5pt, domain=-1:8] coordinates{(3,-2) (3,6)};
        \end{axis}
      \end{tikzpicture}
    &
    \stepcounter{graph}\thegraph &
      \begin{tikzpicture}[
        scale = 0.8,
      ]
        \begin{axis}
          \addplot[line width=1.5pt, domain=-4:1]{-2*x-1};
        \end{axis}
      \end{tikzpicture}
  \end{tabular}
\end{figure}

\end{document}

答案2

首先,我缩小了图的大小。其次,[scale only axis]提供更一致的结果。最后,我相对于轴的左下角在两个方向上将边界框扩大了 5pt。这可以补偿由于边缘文本而导致的轻微变化。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{exercise}
\usepackage{amsmath}
\usepackage{systeme}

\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\usepackage{tikz}
\pgfplotsset{width=3cm, height=3cm, compat=1.18, scale only axis} 

\begin{document}
Write the equation of the lines in the following graphs.
    \begin{multicols}{3}
        \begin{enumerate}
             \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        xmin=-1,
                        xmax=6,
                        ymin=-8,
                        ymax=2,
                        %enlargelimits={abs=0.5},
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-1:4]{3*x-7};
                    \end{axis}
                    \path (-5pt,-5pt) (3cm+5pt,3cm+5pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        ymin=-1,
                        ymax=6,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-4:4]{2};
                    \end{axis}
                    \path (-5pt,-5pt) (3cm+5pt,3cm+5pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        ymin=-2,
                        ymax=6,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-1:8]{-(2/3)*x+4};
                    \end{axis}
                    \path (-5pt,-5pt) (3cm+5pt,3cm+5pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        ymin=-3,
                        ymax=7,
                        xmin=-4,
                        xmax=4,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-4:4]{(4/3)*x+2};
                    \end{axis}
                    \path (-5pt,-5pt) (3cm+5pt,3cm+5pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        xmin=-1,
                        xmax=5,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-1:8] coordinates{(3,-3) (3,3)};
                    \end{axis}
                    \path (-5pt,-5pt) (3cm+5pt,3cm+5pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(current bounding box.north),>=latex]
                    \begin{axis}[
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        xmin=-4,
                        xmax=3,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-4:1]{-2*x-1};
                    \end{axis}
                    \path (-5pt,-5pt) (3cm+5pt,3cm+5pt);
                \end{tikzpicture}
        \end{enumerate}
    \end{multicols}
\end{document}``

演示


此版本使用 [name=border] 将基线放在轴本身上,而不是边界框上。您仍需要向东和向西扩展边界框。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{exercise}
\usepackage{amsmath}
\usepackage{systeme}

\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\usepackage{tikz}
\pgfplotsset{width=3cm, height=3cm, compat=1.18, scale only axis} 

\begin{document}
Write the equation of the lines in the following graphs.
    \begin{multicols}{3}
        \begin{enumerate}
             \item 
                \begin{tikzpicture}[baseline=(border.north),>=latex]
                    \begin{axis}[
                        name=border,
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        xmin=-1,
                        xmax=6,
                        ymin=-8,
                        ymax=2,
                        %enlargelimits={abs=0.5},
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-1:4]{3*x-7};
                    \end{axis}
                    \path (-5pt,0pt) (border.north east)++(5pt,0pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(border.north),>=latex]
                    \begin{axis}[
                        name=border,
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        ymin=-1,
                        ymax=6,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-4:4]{2};
                    \end{axis}
                    \path (-5pt,0pt) (border.north east)++(5pt,0pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(border.north),>=latex]
                    \begin{axis}[
                        name=border,
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        ymin=-2,
                        ymax=6,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-1:8]{-(2/3)*x+4};
                    \end{axis}
                    \path (-5pt,0pt) (border.north east)++(5pt,0pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(border.north),>=latex]
                    \begin{axis}[
                        name=border,
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        ymin=-3,
                        ymax=7,
                        xmin=-4,
                        xmax=4,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-4:4]{(4/3)*x+2};
                    \end{axis}
                    \path (-5pt,0pt) (border.north east)++(5pt,0pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(border.north),>=latex]
                    \begin{axis}[
                        name=border,
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        xmin=-1,
                        xmax=5,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-1:8] coordinates{(3,-3) (3,3)};
                    \end{axis}
                    \path (-5pt,0pt) (border.north east)++(5pt,0pt);
                \end{tikzpicture}
            \item 
                \begin{tikzpicture}[baseline=(border.north),>=latex]
                    \begin{axis}[
                        name=border,
                        grid=both,
                        axis lines=middle,
                        minor tick num=1,
                        ytick distance=2,
                        xmin=-4,
                        xmax=3,
                        axis line style={latex-latex},
                        ticklabel style={font=\tiny,fill=white}
                            ]
                        \addplot[line width=1.5pt, domain=-4:1]{-2*x-1};
                    \end{axis}
                    \path (-5pt,0pt) (border.north east)++(5pt,0pt);
                \end{tikzpicture}
        \end{enumerate}
    \end{multicols}
\end{document}

相关内容