\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
view={110}{45},
hide x axis,
hide z axis,
hide y axis,
]
\addplot3[only marks,scatter,mark=cube*,mark size=20] coordinates {%
(0,0,0) (0,0,1) (0,0,2) (1,0,0) (1,0,1) (1,0,2) (2,0,0) (2,0,1) (2,0,2)
(0,1,0) (0,1,1) (0,1,2) (1,1,0) (1,1,1) (1,1,2) (2,1,0) (2,1,1) (2,1,2)
(0,2,0) (0,2,1) (0,2,2) (1,2,0) (1,2,1) (1,2,2) (2,2,0) (2,2,1) (2,2,2)
} ;
\end{axis}
\end{tikzpicture}
\end{document}
编辑1
使用 xmin、xmax...和视图进行经验尝试,但如果有人知道更科学的方法......无论如何在 z 方向上似乎太长了?
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=3.5,ymin=0,ymax=7.5,zmin=0,zmax=5.5,
view={113}{20},
hide x axis,
hide z axis,
hide y axis,
]
\addplot3[only marks,scatter,mark=cube*,mark size=20] coordinates {%
(0,0,0) (0,0,1) (0,0,2) (1,0,0) (1,0,1) (1,0,2) (2,0,0) (2,0,1) (2,0,2)
(0,1,0) (0,1,1) (0,1,2) (1,1,0) (1,1,1) (1,1,2) (2,1,0) (2,1,1) (2,1,2)
(0,2,0) (0,2,1) (0,2,2) (1,2,0) (1,2,1) (1,2,2) (2,2,0) (2,2,1) (2,2,2)
} ;
\end{axis}
\end{tikzpicture}
\end{document}
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\newcommand{\MyCube}[1][]{%
\begin{scope}[very thin,line cap=round,line join=round,#1]
\draw[fill=blue!20] (0,0,0)--(1,0,0)--(1,1,0)--(0,1,0)--cycle ;
\draw[fill=red!20] (1,0,0)--(1,0,1)--(1,1,1)--(1,1,0)--cycle ;
\draw[fill=yellow!20] (0,1,0)--(0,1,1)--(1,1,1)--(1,1,0)--cycle ;
\end{scope}
}
\newcommand{\LineCubeX}[3][]{%
\begin{scope}[shift={#2}]
\foreach \x in {0,1,...,\the\numexpr#3-1} {%
\MyCube[shift={(\x,0,0)},#1] }
\end{scope}
}
\newcommand{\LineCubeY}[3][]{%
\begin{scope}[shift={#2}]
\foreach \y in {0,1,...,\the\numexpr#3-1} {%
\MyCube[shift={(0,\y,0)},#1] }
\end{scope}
}
\newcommand{\LineCubeZ}[3][]{%
\begin{scope}[shift={#2}]
\foreach \z in {\the\numexpr#3-1,\the\numexpr#3-2,...,0} {%
\MyCube[shift={(0,0,\z)},#1] }
\end{scope}
}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[z=.4cm]
\foreach \z in {3,2,1,0} {%
\foreach \y in {0,1,2,3} {%
\foreach \x in {0,1,2,3} {%
\MyCube[shift={(\x,\y,\z)},opacity=.7] }}}
\end{tikzpicture}
\begin{tikzpicture}[z=.4cm]
% Face arrière
\LineCubeX{(0,0,8)}{9} ;
\LineCubeY{(0,1,8)}{7} ;
\LineCubeY{(8,1,8)}{7} ;
\LineCubeX{(0,8,8)}{9} ;
% montants horizontaux
\LineCubeZ{(0,0,1)}{7} ;
\LineCubeZ{(8,0,1)}{7} ;
\LineCubeZ{(8,8,1)}{7} ;
\LineCubeZ{(0,8,1)}{7} ;
% face avant
\LineCubeX{(0,0,0)}{9} ;
\LineCubeY{(0,1,0)}{7} ;
\LineCubeY{(8,1,0)}{7} ;
\LineCubeX{(0,8,0)}{9} ;
\end{tikzpicture}
\end{document}
答案1
可能有更好的方法来做到这一点,但我使用参考的代码来构建自定义绘图标记。
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
view={110}{45},
hide x axis,
hide z axis,
hide y axis,
]
\pgfdeclareplotmark{realcube}{
\pgfmathsetmacro{\cubex}{100}
\pgfmathsetmacro{\cubey}{100}
\pgfmathsetmacro{\cubez}{100}
\draw[fill] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw[fill] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[fill] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
}
\addplot3[only marks,scatter,mark=realcube,] coordinates {%
(0,0,0) (0,0,1) (0,0,2) (1,0,0) (1,0,1) (1,0,2) (2,0,0) (2,0,1) (2,0,2)
(0,1,0) (0,1,1) (0,1,2) (1,1,0) (1,1,1) (1,1,2) (2,1,0) (2,1,1) (2,1,2)
(0,2,0) (0,2,1) (0,2,2) (1,2,0) (1,2,1) (1,2,2) (2,2,0) (2,2,1) (2,2,2)
} ;
\end{axis}
\end{tikzpicture}
\end{document}
我还不知道为什么立方体尺寸必须是 100 而不是 1。仅当散点网格的网格宽度为 1 时,这才有效。