我怎样才能在 tikz 中制作垂直和反向决策树

我怎样才能在 tikz 中制作垂直和反向决策树

我正在尝试制作一个垂直的“反向”决策树,灵感来自

这是我的试验:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows}
\begin{document}
% Define block styles used later
\tikzstyle{sensor}=[draw, fill=blue!20, text width=5em, 
    text centered, minimum height=2.5em,drop shadow]
\tikzstyle{wa} = [sensor, text width=6em, fill=red!20, 
    minimum height=3em, rounded corners, drop shadow]
% Define distances for bordering
\def\blockdist{2.3}
\def\edgedist{2.5}
\begin{tikzpicture}
    \node (wa) [wa]  {NB Best};
    \path (wa.west)+(-3.2,1.5) node (asr1) [sensor] {$NB_1$};
    \path (wa.west)+(-3.2,0.5) node (asr2)[sensor] {$NB_2$};
    \path (wa.west)+(-3.2,-0.5) node (asr3)[sensor] {$NB_3$}; 
    \path (wa.west)+(-3.2,-1.5) node (asr4)[sensor] {$NB_4$};    
   
    \path (wa.east)+(\blockdist,0) node (vote) [sensor] {NB filtered};

    \path [draw, ->] (asr1.east) -- node [above] {} 
        (wa.155) ;
    \path [draw, ->] (asr2.east) -- node [above] {} 
        (wa.170);
    \path [draw, ->] (asr3.east) -- node [above] {} 
        (wa.190);
    \path [draw, ->] (asr4.east) -- node [above] {} 
        (wa.205);
    \path [draw, ->] (wa.east) -- node [above] {} 
        (vote.west);
\end{tikzpicture}
\end{document}

生成结果:

在此处输入图片描述

当我尝试在第一棵树下使用相同的结构时,它们会相互重叠。我不知道该如何修复它,然后转到所需的图表:

在此处输入图片描述

感谢任何帮助!

答案1

使用scopeyshift。这个想法是改变每个块的最小值,对我来说,这是节点的文本,甚至不是它们的名称。然后改变它的高度。我没有删除任何东西,但注释了不必要的内容,以便您有参考,因为我没有更改您的代码。我添加了“最佳模型”节点。

图片:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,decorations.pathreplacing}
\begin{document}
% Define block styles used later
\tikzstyle{sensor}=[draw, fill=blue!20, text width=5em, 
    text centered, minimum height=2.5em,drop shadow]
\tikzstyle{wa} = [sensor, text width=6em, fill=red!20, 
    minimum height=3em, rounded corners, drop shadow]
% Define distances for bordering
\def\blockdist{2.3}
\def\edgedist{2.5}
\begin{tikzpicture}
    
    \node (best) [wa, align = center] at (4,0) {BEST \\ MODEL};
    
    \begin{scope}[yshift = 3cm]
        \node (wa) [wa]  {P};
        \draw[-latex] (wa.east) -- (best);
        %\path (wa.west)+(-3.2,1.5) node (asr1) [sensor] {$NB_1$};
        \path (wa.west)+(-3.2,0.5) node (asr2)[sensor] {$P$};
        %\path (wa.west)+(-3.2,-0.5) node (asr3)[sensor] {$NB_3$}; 
        %\path (wa.west)+(-3.2,-1.5) node (asr4)[sensor] {$NB_4$};    
        
        %\path (wa.east)+(\blockdist,0) node (vote) [sensor] {NB filtered};
        
        %\path [draw, ->] (asr1.east) -- node [above] {} 
        %(wa.155) ;
        \path [draw, ->] (asr2.east) -- node [above] {} 
        (wa.170);
        %\path [draw, ->] (asr3.east) -- node [above] {} 
        %(wa.190);
        %\path [draw, ->] (asr4.east) -- node [above] {} 
        %(wa.205);
        %\path [draw, ->] (wa.east) -- node [above] {} 
        %(vote.west)
    \end{scope}
    
    \begin{scope}[yshift = 0cm]
       \node (wa) [wa]  {NB Best};
       \draw[-latex] (wa.east) -- (best);
        \path (wa.west)+(-3.2,1.5) node (asr1) [sensor] {$NB_1$};
        \path (wa.west)+(-3.2,0.5) node (asr2)[sensor] {$NB_2$};
        \path (wa.west)+(-3.2,-0.5) node (asr3)[sensor] {$NB_3$}; 
        \path (wa.west)+(-3.2,-1.5) node (asr4)[sensor] {$NB_4$};    
        
        %\path (wa.east)+(\blockdist,0) node (vote) [sensor] {NB filtered};
        
        \path [draw, ->] (asr1.east) -- node [above] {} 
        (wa.155) ;
        \path [draw, ->] (asr2.east) -- node [above] {} 
        (wa.170);
        \path [draw, ->] (asr3.east) -- node [above] {} 
        (wa.190);
        \path [draw, ->] (asr4.east) -- node [above] {} 
        (wa.205);
        %\path [draw, ->] (wa.east) -- node [above] {} 
        %(vote.west);
    \end{scope}

    \begin{scope}[yshift = -5cm]
        \node (wa) [wa]  {CMP Best};
        \draw[-latex] (wa.east) -- (best);
        \path (wa.west)+(-3.2,1.5) node (asr1) [sensor] {$CMP_1$};
        \path (wa.west)+(-3.2,0.5) node (asr2)[sensor] {$CMP_2$};
        \path (wa.west)+(-3.2,-0.5) node (asr3)[sensor] {$CMP_3$}; 
        \path (wa.west)+(-3.2,-1.5) node (asr4)[sensor] {$CMP_4$};    
        
        %\path (wa.east)+(\blockdist,0) node (vote) [sensor] {CMP filtered};
        
        \path [draw, ->] (asr1.east) -- node [above] {} 
        (wa.155) ;
        \path [draw, ->] (asr2.east) -- node [above] {} 
        (wa.170);
        \path [draw, ->] (asr3.east) -- node [above] {} 
        (wa.190);
        \path [draw, ->] (asr4.east) -- node [above] {} 
        (wa.205);
        %\path [draw, ->] (wa.east) -- node [above] {} 
        %(vote.west);
    \end{scope}
    
    \draw [decorate,decoration={brace,amplitude=5pt,mirror,raise=4ex}]
    (wa.south) --++(4,0) node[midway,yshift=-5em, align = center]{Linear \\ Prediction \\  Filter};
    
\end{tikzpicture}
\end{document}

答案2

与 Jes 的解决方案类似,但forest

\documentclass[border=2mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta, shadows, decorations.pathreplacing}

\begin{document}  
\begin{forest}
    for tree={grow=west, draw, 
         drop shadow, text centered,
        edge=Stealth-, child anchor=east, l sep=1cm,
        },
    where level=2{fill=blue!30, sharp corners, text width=5em, minimum height=2.5em, math content}%
    {text width=6em, minimum height=3em, rounded corners, fill=red!30,}
[BEST\\ MODEL, align=center, name=root
    [P
        [P]]
    [NB Best 
        [NB_1]
        [NB_2]
        [NB_3]
        [NB_4]]
    [CMP Best, name=CMP
        [CMP_1]
        [CMP_2]
        [CMP_3]
        [CMP_4]]]
    \draw [decorate,decoration={brace,amplitude=5pt,mirror,raise=4ex}]
    (CMP.south) --(CMP.south-|root) node[midway,yshift=-5em, align = center]{Linear \\ Prediction \\  Filter};
\end{forest}
\end{document}

在此处输入图片描述

相关内容