
続くこの答え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
は考慮されません。これには、最初の軸の目盛りラベルが左余白に入るという望ましくない副作用がありますが、最初のサブ図の前に水平方向のスペースを追加することでこれを回避できます。(以下のコード例を参照してください。)
また、subfigure
sa を少し狭くして、\hfill
その間にスペースを追加して行を埋めるようにしました。
そうは言っても、個別のキャプションを追加するつもりがないのであれば、その仕事に適さないツールを使用しているということになり、コードが必要以上に長くなってしまうでしょう。groupplots
のライブラリを追加しpgfplots
、同じgroupplot
環境で 3 つの画像すべてを追加することをお勧めします。 (前回の質問ではこれについては考えていませんでした。) を使用groupplots
すると、このようなことが非常に簡単に実行できます。
\captionof
パッケージのの助けを借りて、軸に対して指定された相対位置でcaption
を配置することでサブキャプションを追加することもできます(以下の例を参照)。ただし、正しい相互参照を取得するには、カウンターの操作が必要なようです。\node
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
コード用です。