绘制带网格的 3D 轴

绘制带网格的 3D 轴

我使用的代码生成了 3D 轴,但只有网格出现在 xz 平面上。有什么帮助吗?

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{patterns}

\pgfplotsset{compat = newest}

\begin{document}

    \begin{tikzpicture}
        [cube/.style={very thick,black},
            grid/.style={very thin,gray},
            axis/.style={->,blue,thick}]

    %draw a grid in the x-y plane
    \foreach \x in {-0.5,0,...,2.5}
        \foreach \y in {-0.5,0,...,2.5}
        {
            \draw[grid] (\x,-0.5) -- (\x,2.5);
            \draw[grid] (-0.5,\y) -- (2.5,\y);
        }
    
    %draw a grid in the x-z plane
    \foreach \x in {-0.5,0,...,2.5}
        \foreach \z in {-0.5,0,...,2.5}
        {
            \draw[grid] (\x,-0.5) -- (\x,2.5);
            \draw[grid] (-0.5,\z) -- (2.5,\z);
        }

    %draw a grid in the y-z plane
    \foreach \z in {-0.5,0,...,2.5}
        \foreach \y in {-0.5,0,...,2.5}
        {
            \draw[grid] (\z,-0.5) -- (\z,2.5);
            \draw[grid] (-0.5,\y) -- (2.5,\y);
        }
        
    %draw the axes
    \draw[axis] (0,0,0) -- (3,0,0) node[anchor=west]{$x$};
    \draw[axis] (0,0,0) -- (0,3,0) node[anchor=west]{$y$};
    \draw[axis] (0,0,0) -- (0,0,3) node[anchor=west]{$z$};

    %draw the top and bottom of the cube
    %\draw[cube] (0,0,0) -- (0,2,0) -- (2,2,0) -- (2,0,0) -- cycle;
    %\draw[cube] (0,0,2) -- (0,2,2) -- (2,2,2) -- (2,0,2) -- cycle;

    %draw the edges of the cube
    %\draw[cube] (0,0,0) -- (0,0,2);
    %\draw[cube] (0,2,0) -- (0,2,2);
    %\draw[cube] (2,0,0) -- (2,0,2);
    %\draw[cube] (2,2,0) -- (2,2,2);

    \end{tikzpicture}

\end{document}

答案1

请尝试以下代码: 在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{3d}

\begin{document}
\begin{tikzpicture}[scale=2]
    % Defining the max dimensions
    \def\maxdim{2.5}

    % Drawing grids in x-y, y-z, and x-z planes using foreach
    % x-y plane
    \foreach \x in {0,0.5,...,\maxdim}
        \foreach \y in {0,0.5,...,\maxdim}
        {
            \draw[gray, thin] (\x, 0) -- (\x, \maxdim);
            \draw[gray, thin] (0, \y) -- (\maxdim, \y);
        }

    % y-z plane
    \begin{scope}[canvas is yz plane at x=0]
        \foreach \y in {0,0.5,...,\maxdim}
            \foreach \z in {0,0.5,...,\maxdim}
            {
                \draw[gray, thin] (\y, 0) -- (\y, \maxdim);
                \draw[gray, thin] (0, \z) -- (\maxdim, \z);
            }
    \end{scope}

    % x-z plane
    \begin{scope}[canvas is xz plane at y=0]
        \foreach \x in {0,0.5,...,\maxdim}
            \foreach \z in {0,0.5,...,\maxdim}
            {
                \draw[gray, thin] (\x, 0) -- (\x, \maxdim);
                \draw[gray, thin] (0, \z) -- (\maxdim, \z);
            }
    \end{scope}

    % Drawing axes
    % x-axis
    \draw[-stealth, blue, thick] (0,0,0) -- (\maxdim+0.5,0,0) node[right] {$x$};
    % y-axis
    \draw[-stealth, blue, thick] (0,0,0) -- (0,\maxdim+0.5,0) node[above] {$y$};
    % z-axis
    \draw[-stealth, blue, thick] (0,0,0) -- (0,0,\maxdim+0.5) node[below left] {$z$};
\end{tikzpicture}
\end{document}

相关内容