TikZ/pgfplots: 軸ラベルを非表示にした後、tikzpicture サブ図を均等に分散するにはどうすればよいですか?

TikZ/pgfplots: 軸ラベルを非表示にした後、tikzpicture サブ図を均等に分散するにはどうすればよいですか?

続くこの答え2 番目と 3 番目のサブ図を設定すると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

ここでスペースが異なる理由は、いくつかの要因が組み合わさった結果です。まず、最初のプロットが最後の 2 つのプロットよりも幅が広いため、軸の右側からグラフの端までの距離は、subfigure最初のプロットの方が最後の 2 つのプロットよりも短くなります。

さらに、最後の 2 つのプロットは環境よりも狭くsubfigure、デフォルトではこれらの中のテキストは左揃えになっています。この部分を修正するには、\centering各環境の先頭に追加しますsubfigureが、上記の最初の段落で説明したように、軸 1 と 2 の間と軸 2 と 3 の間の距離を等しくするにはそれだけでは不十分です。

他にも方法があるかもしれません。私はエレガントな解決策を見つけるのが得意ではないのですが、私が考えた回避策は、\begin{tikzpicture}[trim axis left]3 つのプロットすべてに を使用することです。trim axis leftは、境界ボックスを変更するスタイルで、境界ボックスを決定するときに (目盛りラベル、y ラベル)tikzpictureの左側にあるものaxisは考慮されません。これには、最初の軸の目盛りラベルが左余白に入るという望ましくない副作用がありますが、最初のサブ図の前に水平方向のスペースを追加することでこれを回避できます。(以下のコード例を参照してください。)

また、subfiguresa を少し狭くして、\hfillその間にスペースを追加して行を埋めるようにしました。

そうは言っても、個別のキャプションを追加するつもりがないのであれば、その仕事に適さないツールを使用しているということになり、コードが必要以上に長くなってしまうでしょう。groupplotsのライブラリを追加しpgfplots、同じgroupplot環境で 3 つの画像すべてを追加することをお勧めします。 (前回の質問ではこれについては考えていませんでした。) を使用groupplotsすると、このようなことが非常に簡単に実行できます。

\captionofパッケージのの助けを借りて、軸に対して指定された相対位置でcaptionを配置することでサブキャプションを追加することもできます(以下の例を参照)。ただし、正しい相互参照を取得するには、カウンターの操作が必要なようです。\nodetext widthfigure

\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コード用です。

ここに画像の説明を入力してください

関連情報