如何配置 tikz/pgfplots 以对齐纸上的图形

如何配置 tikz/pgfplots 以对齐纸上的图形

我有以下 8 个 eps 图,其中前 4 个是使用以下文档生成的

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{bm}
\usepackage{scalerel}
\usetikzlibrary{arrows,shapes,calc}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{external}
\tikzset{external/system call={pdflatex \tikzexternalcheckshellescape -halt-on-error
    -interaction=batchmode -jobname "\image" "\texsource" && %  or ;
   pdftops -eps "\image".pdf}}
\tikzexternalize
\begin{document}
\begin{tikzpicture}[trim axis left]
\pgfmathsetlengthmacro\MajorTickLength{
    \pgfkeysvalueof{/pgfplots/major tick length} * 0.5
}
\begin{axis}[y label style={at={(axis description                 
    cs:0.17,1)},anchor=south west,rotate=-90, font=\small},
    ylabel=Time, 
    legend style={draw=none, fill=none,font=\small, at={(axis 
    cs:1.6,0.25)},anchor=north west},
    legend cell align={left},
    width=8cm, height=5.5cm,
    xmin=1.4, xmax=18.6,
    ymin=-0.02, ymax=0.25,
    major tick length=\MajorTickLength,
    tick pos = left,
    ytick={0, 0.05, 0.10, 0.15, 0.20},
    xtick={2,4,6,8,10, 12,14,16, 18},
    ticklabel style = {font=\scriptsize},
    xticklabels={ $200$,,$600$,,$1000$,, $1400$,, $1800$},
    yticklabels={$0\%$, $5\%$, $10\%$, $15\%$, $20\%$}]
  \addplot[mark=x] coordinates {
    (2, 0.02)
    (4, 0.04)
    (6, 0.06)
    (8, 0.08)
    (10, 0.10)
    (12, 0.12)
    (14, 0.14)
    (16, 0.16)
    (18, 0.18)
};
\legend{$\textsf{A}$}
\end{axis}
\end{tikzpicture}
\end{document}

其他的都是使用

    \begin{axis}[y label style={at={(axis description                 
        cs:0.17,1)},anchor=south west,rotate=-90, font=\small},
ylabel=Time, 
legend style={draw=none, fill=none,font=\small, at={(axis cs:1.6,0.25)},anchor=north west},
legend cell align={left},
width=8cm, height=5.5cm,
xmin=1.4, xmax=18.6,
ymin=0, ymax=0.25,
major tick length=\MajorTickLength,
tick pos = left,
ytick={0, 0.05, 0.10, 0.15, 0.20},
xtick={2,4, 6,8,10,12, 14, 16,18},
ticklabel style = {font=\scriptsize},
xticklabels={ $2 \times 10^4$,, $6 \times 10^4$,,$10 \times 10^4$,, $14 \times 10^4$, ,$18 \times 10^4$},
yticklabels={$0$, $500$, $1000$, $1500$, $2000$}]

唯一的区别是刻度标记。当我将这些图放入论文中时,我发现这些图对齐得不太好。具体来说,最后 4 个图向左移动了一点。 在此处输入图片描述

在论文中插入图片的代码如下。

\documentclass{article}


\usepackage{epsfig}
\usepackage{subcaption}

\begin{document}  
\begin{figure*}[!th]
\centering
\begin{subfigure}{0.24\textwidth}
\centering
\epsfig{file=fig/test1-figure0.eps,scale=0.35}
\caption{\footnotesize{A}}
\end{subfigure}
\begin{subfigure}{0.24\textwidth}
\centering
\epsfig{file=fig/test1-figure0.eps,scale=0.35}
\caption{\footnotesize{A}}
\end{subfigure}
\begin{subfigure}{0.24\textwidth}
\centering
\epsfig{file=fig/test1-figure0.eps,scale=0.35}
\caption{\footnotesize{A}}
\end{subfigure}
\begin{subfigure}{0.24\textwidth}
\centering
\epsfig{file=fig/test1-figure0.eps,scale=0.35}
\caption{\footnotesize{A}}
\end{subfigure}
\begin{subfigure}{0.24\textwidth}
\centering
\epsfig{file=fig/test2-figure0.eps,scale=0.35}
\caption{\footnotesize{B}}
\end{subfigure}
\begin{subfigure}{0.24\textwidth}
\centering
\epsfig{file=fig/test2-figure0.eps,scale=0.35}
\caption{\footnotesize{B}}
\end{subfigure}
\begin{subfigure}{0.24\textwidth}
\centering
\epsfig{file=fig/test2-figure0.eps,scale=0.35}
\caption{\footnotesize{B}}
\end{subfigure}
\begin{subfigure}{0.24\textwidth}
\centering
\epsfig{file=fig/test2-figure0.eps,scale=0.35}
\caption{\footnotesize{B}}
\end{subfigure}
\end{figure*}
\end{document}

答案1

图表的宽度不同。第二行的 y 标签更宽。您可以通过[trim axis left]向图表添加选项来消除这种差异的影响:

\begin{tikzpicture}[trim axis left]

在此处输入图片描述

为了获得进一步的帮助,您需要提供一个 MWE(最小工作示例),这是一个虽小但完整的文档,我们可以编译和观察它,看看您是否还存在一些不匹配的情况。

附录: 我将按照以下方式将您的图像包含在文档中:

\documentclass{article}
\usepackage[margin=20mm]{geometry}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{subcaption}

\begin{document}
    \begin{figure}
\pgfmathsetlengthmacro\MajorTickLength{
    \pgfkeysvalueof{/pgfplots/major tick length} * 0.5
}
\pgfplotsset{
    width=1.1\linewidth, height=.8\linewidth,
    legend style={draw=none, fill=none, font=\scriptsize,
                  legend pos=north west},
    legend cell align=left,
    tick pos = left,
    ticklabel style = {font=\scriptsize},
    major tick length=\MajorTickLength,
    ylabel=Time,
    y label style={at={(0.17,1)},
                   anchor=south west,rotate=-90, font=\footnotesize},
    ytick={0, 0.05,..., 0.20},
    xtick={2,4,..., 18},
    xmin=1.4,   xmax=18.6,
    ymin=0,     ymax=0.25,
           }

\pgfplotsset{% for the first row
    xticklabels={$200$,,$600$,,$1000$,,$1400$,,$1800$},
    yticklabels={$0\%$, $5\%$, $10\%$, $15\%$, $20\%$},
            }
\begin{subfigure}{0.25\linewidth}
\hspace*{2em}
    \begin{tikzpicture}[trim axis left]
\begin{axis}
  \addplot[mark=x] coordinates {
    (2, 0.02)
    (4, 0.04)
    (6, 0.06)
    (8, 0.08)
    (10, 0.10)
    (12, 0.12)
    (14, 0.14)
    (16, 0.16)
    (18, 0.18)
};
\legend{$\textsf{A}$}
\end{axis}
    \end{tikzpicture}
\caption{diagram 1}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
\hspace*{2em}
    \begin{tikzpicture}[trim axis left]
\begin{axis}
  \addplot[mark=x] coordinates {
    (2, 0.02)
    (4, 0.04)
    (6, 0.06)
    (8, 0.08)
    (10, 0.10)
    (12, 0.12)
    (14, 0.14)
    (16, 0.16)
    (18, 0.18)
};
\legend{$\textsf{A}$}
\end{axis}
    \end{tikzpicture}
\caption{diagram 2}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
\hspace*{2em}
    \begin{tikzpicture}[trim axis left]
\begin{axis}
  \addplot[mark=x] coordinates {
    (2, 0.02)
    (4, 0.04)
    (6, 0.06)
    (8, 0.08)
    (10, 0.10)
    (12, 0.12)
    (14, 0.14)
    (16, 0.16)
    (18, 0.18)
};
\legend{$\textsf{A}$}
\end{axis}
    \end{tikzpicture}
\caption{diagram 3}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
\hspace*{2em}
    \begin{tikzpicture}[trim axis left]
\begin{axis}
  \addplot[mark=x] coordinates {
    (2, 0.02)
    (4, 0.04)
    (6, 0.06)
    (8, 0.08)
    (10, 0.10)
    (12, 0.12)
    (14, 0.14)
    (16, 0.16)
    (18, 0.18)
};
\legend{$\textsf{A}$}
\end{axis}
    \end{tikzpicture}
\caption{diagram 4}
\end{subfigure}



\pgfplotsset{% for the secon row   
    xticklabels={$ 2{\times}10^4$,,$6{\times}10^4$,,$10{\times}10^4$,,
                 $14{\times}10^4$,,$18{\times}10^4$},
    xticklabel style = {font=\scriptsize, rotate=45, anchor=north east},
    yticklabels={$0$, $500$, $1000$, $1500$, $2000$}
            }
\medskip
\begin{subfigure}{0.25\linewidth}
\hspace*{2em}
    \begin{tikzpicture}[trim axis left]
\begin{axis}
  \addplot[mark=x] coordinates {
    (2, 0.02)
    (4, 0.04)
    (6, 0.06)
    (8, 0.08)
    (10, 0.10)
    (12, 0.12)
    (14, 0.14)
    (16, 0.16)
    (18, 0.18)
};
\legend{$\textsf{A}$}
\end{axis}
    \end{tikzpicture}
\caption{diagram 5}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
\hspace*{2em}
    \begin{tikzpicture}[trim axis left]
\begin{axis}
  \addplot[mark=x] coordinates {
    (2, 0.02)
    (4, 0.04)
    (6, 0.06)
    (8, 0.08)
    (10, 0.10)
    (12, 0.12)
    (14, 0.14)
    (16, 0.16)
    (18, 0.18)
};
\legend{$\textsf{A}$}
\end{axis}
    \end{tikzpicture}
\caption{diagram 6}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
\hspace*{2em}
    \begin{tikzpicture}[trim axis left]
\begin{axis}
  \addplot[mark=x] coordinates {
    (2, 0.02)
    (4, 0.04)
    (6, 0.06)
    (8, 0.08)
    (10, 0.10)
    (12, 0.12)
    (14, 0.14)
    (16, 0.16)
    (18, 0.18)
};
\legend{$\textsf{A}$}
\end{axis}
    \end{tikzpicture}
\caption{diagram 7}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
\hspace*{2em}
    \begin{tikzpicture}[trim axis left]
\begin{axis}
  \addplot[mark=x] coordinates {
    (2, 0.02)
    (4, 0.04)
    (6, 0.06)
    (8, 0.08)
    (10, 0.10)
    (12, 0.12)
    (14, 0.14)
    (16, 0.16)
    (18, 0.18)
};
\legend{$\textsf{A}$}
\end{axis}
    \end{tikzpicture}
\caption{diagram 8}
\end{subfigure}
    \end{figure}
\end{document}

如你看到的:

  • 图像代码直接包含在文档中

  • 图像的字体大小已减小

  • 第二行中的 x 刻度标签旋转 45 度(否则它们重叠)

  • 图像未在subfigure, however, they are shift with\hspace*{2em}` 右侧居中

在此处输入图片描述

(红线表示文本区域边框)

相关内容