TikZ/pgfplots: tikzpcture を親図/サブ図の float 環境に適合させるにはどうすればよいでしょうか?

TikZ/pgfplots: tikzpcture を親図/サブ図の float 環境に適合させるにはどうすればよいでしょうか?

この MWE では、親 float 環境 (または)tikzpictureの割り当てられたスペースをどのように適合させることができますか?figuresubfigure

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

\pgfplotsset{compat=newest}

\begin{document}

\begin{figure}
    \begin{subfigure}[t]{0.3\textwidth}
        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false]
                \addplot graphics [
                    xmin=0,
                    xmax=50,
                    ymin=0,
                    ymax=150,
                ] {example-image-a};
            \end{axis}
        \end{tikzpicture}
    \end{subfigure}
    %
    \begin{subfigure}[t]{0.3\textwidth}
        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false]
                \addplot graphics [
                    xmin=0,
                    xmax=50,
                    ymin=0,
                    ymax=150,
                ] {example-image-b};
            \end{axis}
        \end{tikzpicture}
    \end{subfigure}
    %
    \begin{subfigure}[t]{0.3\textwidth}
        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false]
                \addplot graphics [
                    xmin=0,
                    xmax=50,
                    ymin=0,
                    ymax=150,
                ] {example-image-c};
            \end{axis}
        \end{tikzpicture}
    \end{subfigure}
\end{figure}

\end{document}

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


編集

以下のコードの出力では、\textwidth各サブ図の幅を に等しくしているにもかかわらず、サブ図が全体に広がることはありません0.33\textwidth

\begin{figure}
    \centering
    \begin{subfigure}[t]{0.33\textwidth}
        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\textwidth]
                \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=\textwidth]
                \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=\textwidth]
                \addplot graphics [
                    xmin=0,
                    xmax=50,
                    ymin=0,
                    ymax=150,
                ] {example-image-c};
            \end{axis}
        \end{tikzpicture}
    \end{subfigure}
\end{figure}

\lipsum[1]

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

答え1

3番目のサブ図が別の行に表示される理由は、サブ図の間にスペースがあるためです。コード内の改行はスペースと同じなので、%後に が必要です\end{subfigure}。つまり、

\end{subfigure}
%
\begin..

必要なのは

\end{subfigure}%
%
\begin..

または

\end{subfigure}%
\begin..

例えば%行末のパーセント記号 ( ) は何の目的で使用されますか?

2 番目の問題は、widtha のキーが pgfplots axis実際には正確ではないことです。 正確でpgfplotsはないのは、目盛りラベルと軸ラベルが軸自体の外側に 45 ポイントのスペースを占めると想定していることです。 したがって、指定された幅から 45 ポイントを減算し、この幅を軸に使用します。

45pt を他の値に変更することはできませんが、独自の軸幅を計算し、キーを使用して、目盛りラベル/軸ラベルを無視してパラメータが軸に適用されるscale only axisように指定できますwidth

もっと大雑把な方法は、全体tikzpictureを に入れて\resizebox、それをサブ図の幅に合わせて単純に拡大することです。この場合の問題は、フォントのサイズも変更されることです。

最後に、これらにキャプションがない場合、subfigure環境は無意味であるため、破棄できることに注意してください。

最初の行は の効果を示し\resizebox、2 行目では軸に計算された幅を使用し、3 行目ではsubfigure環境なしで同じものを使用しています。showframeパッケージはテキスト領域の周囲にフレームを出力します。スクリーンショットに表示されている行は次のとおりです。

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

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

\pgfplotsset{compat=newest}

\begin{document}
\begin{figure}
    \centering
    \begin{subfigure}[t]{0.33\textwidth}
        \resizebox{\textwidth}{!}{\begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\textwidth]
                \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}
        \resizebox{\textwidth}{!}{\begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\textwidth]
                \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}
        \resizebox{\textwidth}{!}{\begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\textwidth]
                \addplot graphics [
                    xmin=0,
                    xmax=50,
                    ymin=0,
                    ymax=150,
                ] {example-image-c};
            \end{axis}
        \end{tikzpicture}}
    \end{subfigure}

    \pgfmathsetlengthmacro{\myaxiswidth}{0.33\textwidth-width(" 150 ")}% 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,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,scale only axis]
                \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,scale only axis]
                \addplot graphics [
                    xmin=0,
                    xmax=50,
                    ymin=0,
                    ymax=150,
                ] {example-image-c};
            \end{axis}
        \end{tikzpicture}
    \end{subfigure}


        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\myaxiswidth,scale only axis]
                \addplot graphics [
                    xmin=0,
                    xmax=50,
                    ymin=0,
                    ymax=150,
                ] {example-image-a};
            \end{axis}
        \end{tikzpicture}%
        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\myaxiswidth,scale only axis]
                \addplot graphics [
                    xmin=0,
                    xmax=50,
                    ymin=0,
                    ymax=150,
                ] {example-image-b};
            \end{axis}
        \end{tikzpicture}%
        \begin{tikzpicture}
            \begin{axis}[enlargelimits=false,width=\myaxiswidth,scale only axis]
                \addplot graphics [
                    xmin=0,
                    xmax=50,
                    ymin=0,
                    ymax=150,
                ] {example-image-c};
            \end{axis}
        \end{tikzpicture}

\end{figure}

\end{document}

関連情報