Tikz LaTex 中的 3D 箱线图

Tikz LaTex 中的 3D 箱线图

我们怎样才能在 LaTex 中轻松地制作出这些 3D 图?有没有简单的方法可以让我们在那里绘制这个,最后我们可以得到 tikz 代码?任何帮助都将不胜感激

这就是我要的

答案1

带有库的解决方案3d可以是:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,math,3d}
\newcommand{\myCube}[2]{%
%back face
\begin{scope}[canvas is yz plane at x=#1]
\foreach \Hpnt in {1,...,4} {
\coordinate (bfb-\Hpnt) at (\Hpnt-1,0);
\coordinate (bfu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (bfr-\Vpnt) at (3,\Vpnt);
\coordinate (bfl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
%front face
\begin{scope}[canvas is yz plane at x=#1+1]
\foreach \Hpnt in {1,...,4} {
\coordinate (ffb-\Hpnt) at (\Hpnt-1,0);
\coordinate (ffu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (ffr-\Vpnt) at (3,\Vpnt);
\coordinate (ffl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
\draw[fill=#2] (bfu-1) -- (ffu-1) -- (ffb-1) -- (ffb-4) -- (bfb-4) -- (bfu-4) -- cycle;
\foreach \jur in {2,3} {
\draw (bfu-\jur) -- (ffu-\jur) -- (ffb-\jur);
\tikzmath{
integer \jlr;
\jlr=\jur-1;
}
\draw (bfr-\jlr) -- (ffr-\jlr) -- (ffl-\jlr);
}
\draw (ffu-1) -- (ffu-4) -- (ffb-4) (bfu-4) -- (ffu-4);
}
\begin{document}
\begin{tikzpicture}[z={(90:1cm)},y={(-30:1cm)},x={(210:0.75cm)}]
\myCube{-3}{green!50!black}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\myCube{-1.5}{red!80!black}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\myCube{0}{orange}
\draw[orange,dashed,very thick] let
\p1=($(current bounding box.south west)!0.5!(current bounding box.north east)$),
\p2=($(current bounding box.south) - (current bounding box.north)$),
\n1={veclen(\x2,\y2)}
in (\p1) circle[radius=\n1/2+6.28mm]; 
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\node at ($($(ffb-1)!0.5!(ffb-4)$)!2mm!90:(ffb-1)$) {$3$};
\node at ($($(ffb-1)!0.5!(ffu-1)$)!2mm!90:(ffu-1)$) {$3$};

\end{tikzpicture}
\end{document}

堆叠立方体

编辑

要添加更多立方体,您必须添加另一个'\myCube{位置}{颜色}。如果你将它添加到代码末尾,那么立方体将位于前景中,如果你将它添加到代码开头,它将位于背景中。位置立方体的直径必须小于其前面立方体的直径。例如:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,math,3d}
\newcommand{\myCube}[2]{%
%back face
\begin{scope}[canvas is yz plane at x=#1]
\foreach \Hpnt in {1,...,4} {
\coordinate (bfb-\Hpnt) at (\Hpnt-1,0);
\coordinate (bfu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (bfr-\Vpnt) at (3,\Vpnt);
\coordinate (bfl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
%front face
\begin{scope}[canvas is yz plane at x=#1+1]
\foreach \Hpnt in {1,...,4} {
\coordinate (ffb-\Hpnt) at (\Hpnt-1,0);
\coordinate (ffu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (ffr-\Vpnt) at (3,\Vpnt);
\coordinate (ffl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
\draw[fill=#2] (bfu-1) -- (ffu-1) -- (ffb-1) -- (ffb-4) -- (bfb-4) -- (bfu-4) -- cycle;
\foreach \jur in {2,3} {
\draw (bfu-\jur) -- (ffu-\jur) -- (ffb-\jur);
\tikzmath{
integer \jlr;
\jlr=\jur-1;
}
\draw (bfr-\jlr) -- (ffr-\jlr) -- (ffl-\jlr);
}
\draw (ffu-1) -- (ffu-4) -- (ffb-4) (bfu-4) -- (ffu-4);
}
\begin{document}
\begin{tikzpicture}[z={(90:1cm)},y={(-30:1cm)},x={(210:0.75cm)}]
\myCube{-4.5}{blue}%<- another cube on background
\myCube{-3}{green!50!black}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\myCube{-1.5}{red!80!black}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
\myCube{0}{orange}
\node at ($($(ffb-4)!0.5!(bfb-4)$)!2mm!90:(ffb-4)$) {$1$};
%\node at ($($(ffb-1)!0.5!(ffb-4)$)!2mm!90:(ffb-1)$) {$3$};%<- label not visible therefore commented
%\node at ($($(ffb-1)!0.5!(ffu-1)$)!2mm!90:(ffu-1)$) {$3$};%<- label not visible therefore commented
\myCube{1.5}{violet}%<- another cube on foreground
\draw[orange,dashed,very thick] let
\p1=($(current bounding box.south west)!0.5!(current bounding box.north east)$),
\p2=($(current bounding box.south) - (current bounding box.north)$),
\n1={veclen(\x2,\y2)}
in (\p1) circle[radius=\n1/2+6.28mm];%<- since the dashed circle is draw using the bounding box of the picture you must shift here this part of the code

\end{tikzpicture}
\end{document}

前面和后面的立方体

编辑倾斜标注

稍微改变一下宏\我的立方体(参见代码中的注释)你可以得到以下内容:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,math,3d}
\def\OffsetLabels{0.7mm}
\newcommand{\myCube}[5]{%
%back face
\begin{scope}[canvas is yz plane at x=#1]
\foreach \Hpnt in {1,...,4} {
\coordinate (bfb-\Hpnt) at (\Hpnt-1,0);
\coordinate (bfu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (bfr-\Vpnt) at (3,\Vpnt);
\coordinate (bfl-\Vpnt) at (0,\Vpnt);
}
\end{scope}
%front face
\begin{scope}[canvas is yz plane at x=#1+1]
\foreach \Hpnt in {1,...,4} {
\coordinate (ffb-\Hpnt) at (\Hpnt-1,0);
\coordinate (ffu-\Hpnt) at (\Hpnt-1,3);
}
\foreach \Vpnt in {1,2} {
\coordinate (ffr-\Vpnt) at (3,\Vpnt);
\coordinate (ffl-\Vpnt) at (0,\Vpnt);
}
\node[transform shape,anchor=north] at ($($(ffb-1)!0.5!(ffb-4)$)!\OffsetLabels!90:(ffb-1)$) {#3};
\node[transform shape,anchor=east] at ($($(ffb-1)!0.5!(ffu-1)$)!\OffsetLabels!90:(ffu-1)$) {#4};
\end{scope}
\begin{scope}[canvas is yz plane at x=#1+0.5,transform shape]
\node[anchor=west] at ($($(ffb-4)!0.5!(bfb-4)$)!\OffsetLabels!90:(ffb-4)$) {#5};
\end{scope}
\draw[fill=#2] (bfu-1) -- (ffu-1) -- (ffb-1) -- (ffb-4) -- (bfb-4) -- (bfu-4) -- cycle;
\foreach \jur in {2,3} {
\draw (bfu-\jur) -- (ffu-\jur) -- (ffb-\jur);
\tikzmath{
integer \jlr;
\jlr=\jur-1;
}
\draw (bfr-\jlr) -- (ffr-\jlr) -- (ffl-\jlr);
}
\draw (ffu-1) -- (ffu-4) -- (ffb-4) (bfu-4) -- (ffu-4);
}
\begin{document}
\begin{tikzpicture}[z={(90:1cm)},y={(-30:1cm)},x={(210:0.75cm)}]
%macro parameter meaning
%\myCube{position}{color}{bottom label}{left label}{right label}
\myCube{-4.5}{blue}{}{}{blue block}
\myCube{-3}{green!50!black}{}{}{green block}
\myCube{-1.5}{red!80!black}{}{}{red block}
\myCube{0}{orange}{}{}{orange block}
\myCube{1.5}{violet}{3}{3}{violet block}
\draw[orange,dashed,very thick] let
\p1=($(current bounding box.south west)!0.5!(current bounding box.north east)$),
\p2=($(current bounding box.south) - (current bounding box.north)$),
\n1={veclen(\x2,\y2)}
in (\p1) circle[radius=\n1/2+6.28mm];

\end{tikzpicture}
\end{document}

带有倾斜标签的立方体

相关内容