TikZ/pgfplots:如何隐藏轴标签后均匀分布 tikzpicture 子图?

TikZ/pgfplots:如何隐藏轴标签后均匀分布 tikzpicture 子图?

下列的这个答案,在设置ytick=\empty第二和第三个子图时,子图分布不均匀,那么如何通过让所有子图具有相同的宽度并均匀分布来修复此输出textwidth

\RequirePackage{luatex85}
\documentclass{article}
\usepackage{pgfplots,caption,subcaption,mwe,showframe}

\pgfplotsset{compat=newest}

\begin{document}

\begin{figure}
    \centering
    \pgfmathsetlengthmacro{\myaxiswidth}{0.33\textwidth-width(" 300 ")}% subtract width of widest ticklabel, with a space on each side
    \begin{subfigure}[t]{0.33\textwidth}
        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis]
                \addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-a};
            \end{axis}
        \end{tikzpicture}
    \end{subfigure}%
    %
    \begin{subfigure}[t]{0.33\textwidth}
        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis, ytick=\empty]
                \addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-b};
            \end{axis}
        \end{tikzpicture}
    \end{subfigure}%
    %
    \begin{subfigure}[t]{0.33\textwidth}
        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis, ytick=\empty]
                \addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-c};
            \end{axis}
        \end{tikzpicture}
    \end{subfigure}
\end{figure}
\end{document}

在此处输入图片描述

答案1

这里空间不同的原因是多种因素的结合。首先,由于第一块地比后两块地宽,因此第一块地从轴右侧到边缘的距离subfigure小于后两块地。

此外,最后两个图比subfigure环境更窄,默认情况下,其中的文本向左对齐。要修复该部分,请\centering在每个环境的开头添加subfigure,但这不足以使轴 1&2 和 2&3 之间的距离相等,正如上面第一段所述。

现在,可能还有其他方法,我绝对不擅长寻找优雅的解决方案,但我想到的解决方法是用于\begin{tikzpicture}[trim axis left]所有三个图。trim axis left是一种改变边界框的样式,tikzpicture以便在确定边界框时不考虑(ticklabels,ylabel)左侧的任何内容axis。这确实有不良的副作用,即第一个轴的刻度标签可能会进入左边距,但您可以通过在第一个子图之前添加一些水平空间来解决这个问题。(参见下面的代码示例。)

我还将subfiguresa 做得稍微窄一些,并在它们之间使用\hfill以添加填充线条的空间。

话虽如此,如果您不打算添加单独的标题,那么我会说您使用了错误的工具来完成这项工作,并使代码变得比必要的更长。我建议添加库groupplotspgfplots并在同一groupplot环境中添加所有三个图像。(我没有想到你之前的问题。)groupplots使这种事情变得非常容易。

借助\captionofcaption包,您还可以添加子标题,方法是将\nodes放置在text width相对于轴的指定位置,如下例所示。figure不过,似乎需要对计数器进行一些调整才能获得正确的交叉引用。

\RequirePackage{luatex85}
\documentclass{article}
\usepackage{pgfplots,caption,subcaption,showframe}
\captionsetup[subfigure]{labelformat=parens}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=newest}
\usepackage{cleveref}

\begin{document}
\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{groupplot}[
        group style={
          group name=G,
          group size=3 by 1,
          y descriptions at=edge left,
          horizontal sep=10pt % adjust as needed
        },
        enlargelimits=false,
        width=0.33\textwidth-width("300"),
        height=0.3\textheight,
        scale only axis]

      \nextgroupplot
      \addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-a};
      \nextgroupplot
      \addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-b};
      \nextgroupplot
      \addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-c};
    \end{groupplot}
    \refstepcounter{figure} % to get correct 
     \node [text width=0.33\textwidth-width("300"),below right,align=center] at (G c1r1.south west) {\captionof{subfigure}{Subcaption for the first plot\label{a}}};
     \node [text width=0.33\textwidth-width("300"),below right,align=center] at (G c2r1.south west) {\captionof{subfigure}{Subcaption for the second plot}};
     \node [text width=0.33\textwidth-width("300"),below right,align=center] at (G c3r1.south west) {\captionof{subfigure}{Subcaption for the third plot}};
     \addtocounter{figure}{-1}
  \end{tikzpicture}
  \caption{..}
\end{figure}


\begin{figure}
  \centering
  \pgfmathsetlengthmacro{\myaxiswidth}{0.33\textwidth-width("300 ")}
  \hspace*{\dimexpr0.33\textwidth-\myaxiswidth}%
  \begin{subfigure}[t]{0.3\textwidth}
    \centering % added
    \begin{tikzpicture}[trim axis left]
      \begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis]
        \addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-a};
      \end{axis}
    \end{tikzpicture}
    \caption{Subcaption for the first plot}
    \label{b}
  \end{subfigure}%
  \hfill
  \begin{subfigure}[t]{0.3\textwidth}
    \centering % added
    \begin{tikzpicture}[trim axis left]
      \begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis, ytick=\empty]
        \addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-b};
      \end{axis}
    \end{tikzpicture}
    \caption{Subcaption for the second plot}
  \end{subfigure}%
  \hfill
  \begin{subfigure}[t]{0.3\textwidth}
    \centering % added
    \begin{tikzpicture}[trim axis left]
      \begin{axis}[enlargelimits=false,width=\myaxiswidth, height=0.3\textheight, scale only axis, ytick=\empty]
        \addplot graphics [xmin=0, xmax=50, ymin=0, ymax=150] {example-image-c};
      \end{axis}
    \end{tikzpicture}
    \caption{Subcaption for the third plot}    
  \end{subfigure}
  \caption{...}
\end{figure}
\end{document}

顶部为groupplots代码,底部为subfigure代码。

在此处输入图片描述

相关内容