我查找并获取了绘制立方体的代码,但现在我无法将其居中。我想将立方体进一步划分为更小的部分,如下所示:
如果我可以只画线而不是小立方体来表示分割,那就没问题了。这是我的图像:
我也无法在图像上添加我想要的标签。我希望能够根据自己的需求为所有三个轴提供标签。这是我的代码,你能帮我吗?
\newcommand{\Depth}{5}
\newcommand{\Height}{5}
\newcommand{\Width}{5}
\begin{tikzpicture}
\centering
\coordinate (O) at (0,0,0);
\coordinate (A) at (0,\Width,0);
\coordinate (B) at (0,\Width,\Height);
\coordinate (C) at (0,0,\Height);
\coordinate (D) at (\Depth,0,0);
\coordinate (E) at (\Depth,\Width,0);
\coordinate (F) at (\Depth,\Width,\Height);
\coordinate (G) at (\Depth,0,\Height);
\draw[] (O) -- (C) -- (G) -- (D) -- cycle;% Bottom Face
\draw[] (O) -- (A) -- (E) -- (D) -- cycle;% Back Face
\draw[] (O) -- (A) -- (B) -- (C) -- cycle;% Left Face
\draw[] (D) -- (E) -- (F) -- (G) -- cycle;% Right Face
\draw[] (C) -- (B) -- (F) -- (G) -- cycle;% Front Face
\draw[] (A) -- (B) -- (F) -- (E) -- cycle;% Top Face
\end{tikzpicture}
答案1
只需通过嵌套循环定义您放置的图片即可。着色可以通过\ifnum
s 完成。最麻烦的部分是从屏幕截图中插入文本。
\documentclass[tikz,border=3mm]{standalone}
\definecolor{pft}{RGB}{150,198,145}
\begin{document}
\begin{tikzpicture}[node font=\sffamily,nodes={text depth=0.25ex},
scale=4/3,
pics/block/.style={code={
\tikzset{block/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/block/##1}}%
\colorlet{cc}{\pv{color}}%
\draw[fill=cc] (-\pv{a}/2,-\pv{a}/2,-\pv{a}/2) --
(\pv{a}/2,-\pv{a}/2,-\pv{a}/2) --
(\pv{a}/2,\pv{a}/2,-\pv{a}/2) --
(-\pv{a}/2,\pv{a}/2,-\pv{a}/2) -- cycle;
\draw[fill=cc!70!black] (-\pv{a}/2,-\pv{a}/2,-\pv{a}/2) --
(-\pv{a}/2,-\pv{a}/2,\pv{a}/2) --
(-\pv{a}/2,\pv{a}/2,\pv{a}/2) --
(-\pv{a}/2,\pv{a}/2,-\pv{a}/2) -- cycle;
\draw[fill=cc!60!black] (-\pv{a}/2,-\pv{a}/2,-\pv{a}/2) --
(-\pv{a}/2,-\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,-\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,-\pv{a}/2,-\pv{a}/2) -- cycle;
}},block/.cd,a/.initial=1,color/.initial=gray!40]
\path[transform shape] foreach \Z in {0,...,3}
{foreach \X in {0,...,3}
{foreach \Y in {0,...,3}
{\pgfextra{%
\ifnum\Z=3
\edef\mycolor{blue!70!black}
\ifnum\Y=2
\edef\mycolor{pft}
\fi
\ifnum\X=1
\ifnum\Y=2
\edef\mycolor{orange}
\else
\edef\mycolor{yellow}
\fi
\fi
\else
\edef\mycolor{gray!40}
\ifnum\X=1
\edef\mycolor{yellow}
\fi
\ifnum\Y=2
\edef\mycolor{pft}
\fi
\fi}
(\X,\Y,\Z) pic{block={color=\mycolor}}}}};
% y axis
\path foreach \X [count=\Y] in {Laptop,Mobile,TV,Tablet}
{(-0.7,-1+\Y,3.5) node[rotate=90,anchor=south]{\X}};
\draw[orange!30!yellow,-stealth,ultra thick] (-1.5,-0.5,3.5) --
node[pos=0,sloped,above right,font=\large,black]{PRODUCTS} ++ (0,4.2,0);
% x axis
\path foreach \X [count=\Y] in {USA,Asia,Australia,Europe}
{(-1+\Y,-0.7,3.5) node[anchor=north,text height=0.8em]{\X}};
\draw[orange!30!yellow,-stealth,ultra thick] (-0.5,-1.5,3.5) --
node[pos=0,sloped,below right,font=\large,black]{GEOGRAPHY}
++ (4.2,0,0);
% z axis
\path foreach \Y in {1,...,4}
{(3.5,-0.5,4-\Y) node[anchor=north west,text height=0.8em]{$Q_{\Y}$}};
\draw[orange!30!yellow,-stealth,ultra thick] (4,-1.5,3.5) --
node[pos=0,sloped,below right,font=\large,black]{time}
++ (0,0,-4.2);
\end{tikzpicture}
\end{document}
如果您想知道是否可以使用更现实的投影,答案是肯定的。
这是一个带有完整立方体的版本。
\documentclass[tikz,border=3mm]{standalone}
\definecolor{pft}{RGB}{150,198,145}
\begin{document}
\begin{tikzpicture}[node font=\sffamily,nodes={text depth=0.25ex},
scale=4/3,
pics/block/.style={code={
\tikzset{block/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/block/##1}}%
\colorlet{cc}{\pv{color}}%
\draw[fill=cc] (-\pv{a}/2,-\pv{a}/2,-\pv{a}/2) --
(\pv{a}/2,-\pv{a}/2,-\pv{a}/2) --
(\pv{a}/2,\pv{a}/2,-\pv{a}/2) --
(-\pv{a}/2,\pv{a}/2,-\pv{a}/2) -- cycle;
\draw[fill=cc!70!black] (-\pv{a}/2,-\pv{a}/2,-\pv{a}/2) --
(-\pv{a}/2,-\pv{a}/2,\pv{a}/2) --
(-\pv{a}/2,\pv{a}/2,\pv{a}/2) --
(-\pv{a}/2,\pv{a}/2,-\pv{a}/2) -- cycle;
\draw[fill=cc!60!black] (-\pv{a}/2,-\pv{a}/2,-\pv{a}/2) --
(-\pv{a}/2,-\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,-\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,-\pv{a}/2,-\pv{a}/2) -- cycle;
\draw[fill=cc!70!black] (\pv{a}/2,-\pv{a}/2,-\pv{a}/2) --
(\pv{a}/2,-\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,\pv{a}/2,-\pv{a}/2) -- cycle;
\draw[fill=cc!60!black] (-\pv{a}/2,\pv{a}/2,-\pv{a}/2) --
(-\pv{a}/2,\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,\pv{a}/2,-\pv{a}/2) -- cycle;
\draw[fill=cc] (-\pv{a}/2,-\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,-\pv{a}/2,\pv{a}/2) --
(\pv{a}/2,\pv{a}/2,\pv{a}/2) --
(-\pv{a}/2,\pv{a}/2,\pv{a}/2) -- cycle;
}},block/.cd,a/.initial=1,color/.initial=gray!40]
\path[transform shape] foreach \Z in {0,...,3}
{foreach \X in {0,...,3}
{foreach \Y in {0,...,3}
{\pgfextra{%
\ifnum\Z=3
\edef\mycolor{blue!70!black}
\ifnum\Y=2
\edef\mycolor{pft}
\fi
\ifnum\X=1
\ifnum\Y=2
\edef\mycolor{orange}
\else
\edef\mycolor{yellow}
\fi
\fi
\else
\edef\mycolor{gray!40}
\ifnum\X=1
\edef\mycolor{yellow}
\fi
\ifnum\Y=2
\edef\mycolor{pft}
\fi
\fi}
(\X,\Y,\Z) pic{block={color=\mycolor}}}}};
% y axis
\path foreach \X [count=\Y] in {Laptop,Mobile,TV,Tablet}
{(-0.7,-1+\Y,3.5) node[rotate=90,anchor=south]{\X}};
\draw[orange!30!yellow,-stealth,ultra thick] (-1.5,-0.5,3.5) --
node[pos=0,sloped,above right,font=\large,black]{PRODUCTS} ++ (0,4.2,0);
% x axis
\path foreach \X [count=\Y] in {USA,Asia,Australia,Europe}
{(-1+\Y,-0.7,3.5) node[anchor=north,text height=0.8em]{\X}};
\draw[orange!30!yellow,-stealth,ultra thick] (-0.5,-1.5,3.5) --
node[pos=0,sloped,below right,font=\large,black]{GEOGRAPHY}
++ (4.2,0,0);
% z axis
\path foreach \Y in {1,...,4}
{(3.5,-0.5,4-\Y) node[anchor=north west,text height=0.8em]{$Q_{\Y}$}};
\draw[orange!30!yellow,-stealth,ultra thick] (4,-1.5,3.5) --
node[pos=0,sloped,below right,font=\large,black]{time}
++ (0,0,-4.2);
\end{tikzpicture}
\end{document}
请注意,pgfplots
有一个3d cube
情节标记,您可以使用它以更结构化的方式获取类似的情节。