讓 3D 圖形的邊界框更緊密

讓 3D 圖形的邊界框更緊密

我正在嘗試使用 pgfplots 繪製 3 個數字並排繪製。為此,我創建了 3 個帶有 of 的小頁面0.3\textwidth,以便它們之間有一些空間。

然後,在每個小頁中,我繪製以下管子(此處作為 MWE,僅包含一個管)

\documentclass{article}
\usepackage{pgfplots}
\usepackage[showframe]{geometry}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
        name=plot2,
        axis lines=middle, ticks=none,
        width=\textwidth,
        zmin=0, zmax=6,
        xmin=-3, xmax=3,
        ymin=-3, ymax=3,
        xlabel={$X_1$}, ylabel={$X_2$}, zlabel={$t$},
        title={TDSL}
        ]

        \addplot3[%
            opacity = 0.02,
            fill opacity=0.5,
        mesh/interior colormap name=hot,
        surf,
        colormap/hot,
        faceted color=black,
        z buffer = sort,
        samples = 20,
        variable = \u,
        variable y = \v,
        domain = 0:360,
        y domain = 0:5,
        ]
        ({cos(u)}, {sin(u)}, {v});
    \end{axis}
\end{tikzpicture}
\end{document}

根據它width=\textwidth,應該佔據整個寬度,但事實並非如此。此外,標題「TDSL」位於圖上方非常高的位置,就像有許多空白使實際圖更小一樣。

我的問題是,如何使圖形具有指定的寬度?

答案1

我認為你誤解了@user121799(又名@marmot)。一切都按其應有的方式進行。為了說服您,我添加了軸背景顏色並在第一張圖片中顯示了下面的結果,這是以下程式碼的結果。

當您確定不需要仍然位於軸“框”中的所有四個方向的空間時,您可以調整bounding box繪圖的空間。結果如下第二張圖所示。紅色矩形僅用於調試目的,以顯示調整後的邊界框。有關需要執行哪些操作的詳細信息,請查看程式碼中的註釋。

正如您在第二張圖片中看到的那樣,“僅”調整邊界框不會axis\textwidth.因此你必須調整該width手動這樣就真正充分\textwidth利用了。我也在程式碼中對合適的值提出了評論建議。

% used PGFPlots v1.16
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat` level or higher to position axis labels right
        compat=1.8,
        % for simplicity created a style of the original `axis` options
        my axis style/.style={
            width=\textwidth,
            axis lines=middle,
            ticks=none,
            zmin=0, zmax=6,
            xmin=-3, xmax=3,
            ymin=-3, ymax=3,
            xlabel={$X_1$}, ylabel={$X_2$}, zlabel={$t$},
            title={TDSL},
            % -----------------------------------------------------------------
            % (added an axis background color for debugging purposes)
            axis background/.style={
                fill=blue!25,
                opacity=0.5,
            },
            % -----------------------------------------------------------------
        },
        % for simplicity created a style for the `\addplot` command
        my plot style/.style={
            opacity=0.02,
            fill opacity=0.5,
            mesh/interior colormap name=hot,
            surf,
            faceted color=black,
            z buffer=sort,
            samples=20,
            variable=\u,
            variable y=\v,
            domain=0:360,
            y domain=0:5,
        },
        % a style to (almost) achieve what you want
        my advanced axis style/.style={
            my axis style,
%            % because the `width` doesn't know about "correcting" the bounding box
%            % you have to manually adjust the value to fit your needs (again)
%            width=1.5\textwidth,
            title style={
                % move title above z-axis (arrow)
                at={(axis top)},
                % give the title node a name
                % (which is later used to determine the bounding box of the plot)
                name=axis title,
            },
            % define some helper coordinates to determine the needed/wanted bounding box
            execute at end axis={
                \coordinate (axis left)   at (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0,0);
                \coordinate (axis right)  at (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0,0);
                \coordinate (axis top)    at (axis cs:0,0,\pgfkeysvalueof{/pgfplots/zmax});
                %
                \coordinate (axis bottom) at (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin},0);
                \coordinate (axis lower left)  at (axis bottom -| axis left);
%                % for the top coordinate we need to account for the title
%                % unfortunately at this time the `(axis title)` coordinate is unavailable
%                \coordinate (axis upper right) at (axis title.north -| axis right);
            },
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[my axis style]
        \addplot3 [my plot style] ({cos(u)}, {sin(u)}, {v});
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    % don't calculate a bounding box yet
    \begin{pgfinterruptboundingbox}
        % use the modified/advanced axis style here
        \begin{axis}[my advanced axis style]
            \addplot3 [my plot style] ({cos(u)}, {sin(u)}, {v});
        \end{axis}
    \end{pgfinterruptboundingbox}

    % -------------------------------------------------------------------------
    % for debugging only
    \draw [red] (axis lower left) rectangle (axis title.north -| axis right);
    % -------------------------------------------------------------------------
    % now we can set the bounding box using the helper coordinates
    \useasboundingbox (axis lower left) rectangle (axis title.north -| axis right);
\end{tikzpicture}
\end{document}

第一頁結果:

顯示上述程式碼第一頁結果的圖像

第二頁結果:

顯示上述程式碼第二頁結果的圖像

相關內容