使用 LaTeX 中的“pgfplots”删除图形周围的空白或边缘

使用 LaTeX 中的“pgfplots”删除图形周围的空白或边缘

使用 绘图时pgfplots,图形周围会有空白,导致图形无法正确居中。例如:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{etex}
\usepackage[showframe]{geometry}
%\usetikzlibrary{calc,shapes.arrows,datavisualization,arrows.meta,decorations.pathmorphing,backgrounds,positioning,fit,petri,decorations.pathreplacing}
\begin{document}
\begin{figure}
    \centering
    \begin{tabular}{lr}
    &
        \begin{tikzpicture}
          \begin{axis}[xticklabel style={text width=1em},yticklabel style={/pgf/number format/precision =2,/pgf/number format/fixed,/pgf/number format/fixed zerofill},scaled ticks=false,width=7.4cm,height=4cm,xmin=0,xmax=12,ylabel={the first}]
          \addplot[
          scatter,only marks, point meta=explicit symbolic,
          scatter/classes={
            a={mark=o,draw=black},
            b={mark=square*},
            c={mark=triangle*}
            },
            ] table [meta=label]{
                x   y   label
1   0.55476356  c
2   0.363722187 c
3   0.81286394  c
4   0.875783813 c
5   0.315270863 c
6   0.827911581 c
7   0.720070049 c
8   0.347479399 c
9   0.27043531  c
10  0.490422201 c
11  0.69325892  c
1   0.735883237 a
2   0.129003395 a
3   0.461826926 a
4   0.756650854 a
5   0.305755095 a
6   0.263120301 a
7   0.38954705  a
8   0.07894936  a
9   0.984081129 a
10  0.712322139 a
11  0.146583606 a
1   0.186438911 b
2   0.483752955 b
3   0.955453588 b
4   0.931741157 b
5   0.307806207 b
6   0.932585214 b
7   0.390174549 b
8   0.397465905 b
9   0.893646115 b
10  0.177956892 b
11  0.250648427 b
            };
          \addplot[mark=none, black,dashed,domain=0:12,samples=2] {0.336};
          \end{axis}
        \end{tikzpicture}\\
        \begin{tikzpicture}
          \begin{axis}[xticklabel style={text width=1em},yticklabel style={/pgf/number format/precision =2,/pgf/number format/fixed,/pgf/number format/fixed zerofill},scaled ticks=false,width=7.4cm,height=4cm,xmin=0,xmax=12,ylabel={the second} ]
          \addplot[
          scatter,only marks, point meta=explicit symbolic,
          scatter/classes={
            a={mark=o,draw=black},
            b={mark=square*},
            c={mark=triangle*}
            },
            ] table [meta=label]{
                x   y   label
1   0.55476356  c
2   0.363722187 c
3   0.81286394  c
4   0.875783813 c
5   0.315270863 c
6   0.827911581 c
7   0.720070049 c
8   0.347479399 c
9   0.27043531  c
10  0.490422201 c
11  0.69325892  c
1   0.735883237 a
2   0.129003395 a
3   0.461826926 a
4   0.756650854 a
5   0.305755095 a
6   0.263120301 a
7   0.38954705  a
8   0.07894936  a
9   0.984081129 a
10  0.712322139 a
11  0.146583606 a
1   0.186438911 b
2   0.483752955 b
3   0.955453588 b
4   0.931741157 b
5   0.307806207 b
6   0.932585214 b
7   0.390174549 b
8   0.397465905 b
9   0.893646115 b
10  0.177956892 b
11  0.250648427 b

            };
            \addplot[mark=none, black,dashed,domain=0:12,samples=2] {0.778};
          \end{axis}
        \end{tikzpicture}&
        \begin{tikzpicture}
          \begin{axis}[xticklabel style={text width=1em},yticklabel style={/pgf/number format/precision =2,/pgf/number format/fixed,/pgf/number format/fixed zerofill},scaled ticks=false,width=7.4cm,height=4cm,xmin=0,xmax=12,ylabel={the third} ]
          \addplot[
          scatter,only marks, point meta=explicit symbolic,
          scatter/classes={
            a={mark=o,draw=black},
            b={mark=square*},
            c={mark=triangle*}
            },
            ] table [meta=label]{
                x   y   label
1   0.55476356  c
2   0.363722187 c
3   0.81286394  c
4   0.875783813 c
5   0.315270863 c
6   0.827911581 c
7   0.720070049 c
8   0.347479399 c
9   0.27043531  c
10  0.490422201 c
11  0.69325892  c
1   0.735883237 a
2   0.129003395 a
3   0.461826926 a
4   0.756650854 a
5   0.305755095 a
6   0.263120301 a
7   0.38954705  a
8   0.07894936  a
9   0.984081129 a
10  0.712322139 a
11  0.146583606 a
1   0.186438911 b
2   0.483752955 b
3   0.955453588 b
4   0.931741157 b
5   0.307806207 b
6   0.932585214 b
7   0.390174549 b
8   0.397465905 b
9   0.893646115 b
10  0.177956892 b
11  0.250648427 b

            };
            \addplot[mark=none, black,dashed,domain=0:12,samples=2] {0.778};
          \end{axis}
        \end{tikzpicture}\\
    \end{tabular}
\end{figure}
\end{document}

输出:

在此处输入图片描述

如何去掉左边红线标记的空白,让图形整体移动到页面中央?左边的图形需要与左边的垂直线左对齐,右边的图形需要与右边的垂直线右对齐。

答案1

删除tabular环境。如果愿意,您可以在这里使用框或子图来发挥想象力,但\hfill在第一行使用简单的命令就可以完成您想要的工作。

获得额外空间的原因是tabular列之间和列之前/之后的填充。

使用 \hfill 将顶行与右对齐,并使底行不带表格

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage[showframe]{geometry}
%\usetikzlibrary{calc,shapes.arrows,datavisualization,arrows.meta,decorations.pathmorphing,backgrounds,positioning,fit,petri,decorations.pathreplacing}
\begin{document}
\begin{figure}
  \hfill
  \begin{tikzpicture}
    \begin{axis}[xticklabel style={text width=1em},yticklabel style={/pgf/number format/precision =2,/pgf/number format/fixed,/pgf/number format/fixed zerofill},scaled ticks=false,width=7.4cm,height=4cm,xmin=0,xmax=12,ylabel={the first}]
      \addplot[
      scatter,only marks, point meta=explicit symbolic,
      scatter/classes={
        a={mark=o,draw=black},
        b={mark=square*},
        c={mark=triangle*}
      },
      ] table [meta=label]{
        x   y   label
        1   0.55476356  c
        2   0.363722187 c
        3   0.81286394  c
        4   0.875783813 c
        5   0.315270863 c
        6   0.827911581 c
        7   0.720070049 c
        8   0.347479399 c
        9   0.27043531  c
        10  0.490422201 c
        11  0.69325892  c
        1   0.735883237 a
        2   0.129003395 a
        3   0.461826926 a
        4   0.756650854 a
        5   0.305755095 a
        6   0.263120301 a
        7   0.38954705  a
        8   0.07894936  a
        9   0.984081129 a
        10  0.712322139 a
        11  0.146583606 a
        1   0.186438911 b
        2   0.483752955 b
        3   0.955453588 b
        4   0.931741157 b
        5   0.307806207 b
        6   0.932585214 b
        7   0.390174549 b
        8   0.397465905 b
        9   0.893646115 b
        10  0.177956892 b
        11  0.250648427 b
      };
      \addplot[mark=none, black,dashed,domain=0:12,samples=2] {0.336};
    \end{axis}
  \end{tikzpicture}
  
  \centering
  \begin{tikzpicture}
    \begin{axis}[xticklabel style={text width=1em},yticklabel style={/pgf/number format/precision =2,/pgf/number format/fixed,/pgf/number format/fixed zerofill},scaled ticks=false,width=7.4cm,height=4cm,xmin=0,xmax=12,ylabel={the second} ]
      \addplot[
      scatter,only marks, point meta=explicit symbolic,
      scatter/classes={
        a={mark=o,draw=black},
        b={mark=square*},
        c={mark=triangle*}
      },
      ] table [meta=label]{
        x   y   label
        1   0.55476356  c
        2   0.363722187 c
        3   0.81286394  c
        4   0.875783813 c
        5   0.315270863 c
        6   0.827911581 c
        7   0.720070049 c
        8   0.347479399 c
        9   0.27043531  c
        10  0.490422201 c
        11  0.69325892  c
        1   0.735883237 a
        2   0.129003395 a
        3   0.461826926 a
        4   0.756650854 a
        5   0.305755095 a
        6   0.263120301 a
        7   0.38954705  a
        8   0.07894936  a
        9   0.984081129 a
        10  0.712322139 a
        11  0.146583606 a
        1   0.186438911 b
        2   0.483752955 b
        3   0.955453588 b
        4   0.931741157 b
        5   0.307806207 b
        6   0.932585214 b
        7   0.390174549 b
        8   0.397465905 b
        9   0.893646115 b
        10  0.177956892 b
        11  0.250648427 b
        
      };
      \addplot[mark=none, black,dashed,domain=0:12,samples=2] {0.778};
    \end{axis}
  \end{tikzpicture}\hfill
  \begin{tikzpicture}
    \begin{axis}[xticklabel style={text width=1em},yticklabel style={/pgf/number format/precision =2,/pgf/number format/fixed,/pgf/number format/fixed zerofill},scaled ticks=false,width=7.4cm,height=4cm,xmin=0,xmax=12,ylabel={the third} ]
      \addplot[
      scatter,only marks, point meta=explicit symbolic,
      scatter/classes={
        a={mark=o,draw=black},
        b={mark=square*},
        c={mark=triangle*}
      },
      ] table [meta=label]{
        x   y   label
        1   0.55476356  c
        2   0.363722187 c
        3   0.81286394  c
        4   0.875783813 c
        5   0.315270863 c
        6   0.827911581 c
        7   0.720070049 c
        8   0.347479399 c
        9   0.27043531  c
        10  0.490422201 c
        11  0.69325892  c
        1   0.735883237 a
        2   0.129003395 a
        3   0.461826926 a
        4   0.756650854 a
        5   0.305755095 a
        6   0.263120301 a
        7   0.38954705  a
        8   0.07894936  a
        9   0.984081129 a
        10  0.712322139 a
        11  0.146583606 a
        1   0.186438911 b
        2   0.483752955 b
        3   0.955453588 b
        4   0.931741157 b
        5   0.307806207 b
        6   0.932585214 b
        7   0.390174549 b
        8   0.397465905 b
        9   0.893646115 b
        10  0.177956892 b
        11  0.250648427 b
        
      };
      \addplot[mark=none, black,dashed,domain=0:12,samples=2] {0.778};
    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

如果您想要子标题或更精美的对齐,请查看subcaption

相关内容