TikZ/pgfplots:如何讓 tikzpcture 適合父圖/子圖浮動環境?

TikZ/pgfplots:如何讓 tikzpcture 適合父圖/子圖浮動環境?

在這個MWE中,我要如何適應父親浮動環境(或)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

將第三個子圖放在單獨的行上的原因是子圖之間有空格。程式碼中的換行符與空格相同,因此您需要一個%after \end{subfigure}。即,而不是

\end{subfigure}
%
\begin..

你需要

\end{subfigure}%
%
\begin..

或者

\end{subfigure}%
\begin..

參見例如%行尾的百分號 ( ) 有什麼用?

第二個問題是widtha 的鍵 pgfplots axis其實並不準確。假設pgfplots刻度標籤和軸標籤佔據軸本身以外 45pt 的空間。因此它採用指定的寬度,減去 45pt,並使用該寬度作為軸。

您無法將 45pt 變更為其他值,但您可以計算自己的軸寬度,並使用 鍵scale only axis表示該width參數適用於軸,而不考慮刻度標籤/軸標籤。

更粗略的方法是將整體放入tikzpicturea 中\resizebox,然後簡單地將其放大到子圖的寬度。這樣做的問題是字體也會調整大小。

最後,請注意,如果您沒有這些標題,那麼這些subfigure環境就毫無意義,可以丟棄。

第一行顯示了 的效果\resizebox,在第二行我使用了計算出的軸寬度,在第三行相同,但沒有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}

相關內容