使 3D 图形的边界框更紧密

使 3D 图形的边界框更紧密

我正在尝试使用 pgfplots 绘制 3 个并排的图形。为此,我创建了 3 个带有 的小页面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}

第一页结果:

该图显示了上述代码第一页的结果

第二页结果:

该图显示了上述代码的第二页的结果

相关内容