带有级别标签的水平概率树

带有级别标签的水平概率树

我正在尝试使用 tikz(不是 tikz-qtree)创建带有级别标签的水平概率树。现在,我有一个水平树。标签未与图表顶部对齐。我希望所有标签都对齐,而无需手动调整位置。

以下是 MWE:

\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=4.5cm, sibling distance=1.5cm]
\tikzstyle{level 3}=[level distance=3.5cm, sibling distance=2cm]


\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [rectangle, draw=none, minimum width=3pt, inner sep=0pt]

\tikzstyle{ans} = [color=red]


\begin{tikzpicture}[level distance=5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1.2cm},
level 3/.style={level distance = 2cm},grow'=right]
\tikzstyle{every node}=[]
    \node (Root) [] {Origin}
        child [] {
        node {Door A}
        child { node {A} 
                child {node[end] {$0$} }
                edge from parent
                node[left] {$0$}
        }
        child [black] { node {B} 
                child {node[ans,end] {$\frac{1}{6}$} }
                edge from parent
                node[ans,left] {$\frac{1}{2}$}
        }
        child [black] { node {C} 
                child {node[ans,end] {$\frac{1}{6}$} }
            edge from parent
            node[ans,left] {$\frac{1}{2}$}
        }
        edge from parent
        node[above] {$\frac{1}{3}$}
    }
    child {
        node {Door B}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B} 
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {C} 
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    }
    child {
        node {Door C}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B}
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        child { node {C}
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    };
   % How I'm applying labels to each level. 
  % Need to be able to dynamically align nodes at top level
\begin{scope}[every node/.style={above}]
 \path (Root    -| Root)  ++(0,15mm) node {Test};
 \path (Root-1   -| Root-1)  ++(0,15mm) node {Contestant Chooses};
 \path (Root-1-1  -| Root-3-3-1)  ++(10mm,13mm) node {Host Reveals Door};
 \path (Root-1-1-1 -| Root-3-3) ++(0,15mm) node {Probability};
\end{scope}


\end{tikzpicture}

我明白了

在此处输入图片描述

并且想要这个 在此处输入图片描述

答案1

您可以使用intersections坐标系来对齐所有级别的标签。首先将最顶部叶子的标签放置在您想要的位置:

\node[above= 1cm of Root-1-1-1] (labels-level) {Host Reveals Door};

然后将其用作所有其他标签的水平参考

\node[at =(labels-level-|Root-1-1)] {Probability};
\node[at =(labels-level-|Root-1)] {Contestant Chooses};
\node[at =(labels-level-|Root)] {Test};

我使用positioning库来固定第一个标签的位置。完整代码如下

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}

\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=4.5cm, sibling distance=1.5cm]
\tikzstyle{level 3}=[level distance=3.5cm, sibling distance=2cm]


\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [rectangle, draw=none, minimum width=3pt, inner sep=0pt]

\tikzstyle{ans} = [color=red]


\begin{tikzpicture}[level distance=5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1.2cm},
level 3/.style={level distance = 2cm},grow'=right]
\tikzstyle{every node}=[]
    \node (Root) [] {Origin}
        child [] {
        node {Door A}
        child { node {A} 
                child {node[end] {$0$} }
                edge from parent
                node[left] {$0$}
        }
        child [black] { node {B} 
                child {node[ans,end] {$\frac{1}{6}$} }
                edge from parent
                node[ans,left] {$\frac{1}{2}$}
        }
        child [black] { node {C} 
                child {node[ans,end] {$\frac{1}{6}$} }
            edge from parent
            node[ans,left] {$\frac{1}{2}$}
        }
        edge from parent
        node[above] {$\frac{1}{3}$}
    }
    child {
        node {Door B}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B} 
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {C} 
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    }
    child {
        node {Door C}
        child { node {A} 
                child {node[ans,end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        child { node {B}
                child {node[ans,end] {$\frac{1}{3}$} }
                edge from parent
                node[ans,left] {$1$}
        }
        child { node {C}
                child {node[end] {$0$} }
                edge from parent
                node[ans,left] {$0$}
        }
        edge from parent
        node[ans,left] {$\frac{1}{3}$}
    };
   % How I'm applying labels to each level. 
  % Need to be able to dynamically align nodes at top level
\begin{scope}[every node/.style={text width=2cm, align=center, anchor=center, font=\bfseries,}]
 \node[above= 1cm of Root-1-1-1] (labels-level) {Host Reveals Door};
 \node[at =(labels-level-|Root-1-1)] {Probability};
 \node[at =(labels-level-|Root-1)] {Contestant Chooses};
 \node[at =(labels-level-|Root)] {Test};

\end{scope}
\end{tikzpicture}
\end{document}

和结果(根据需要更改标签方面)

在此处输入图片描述

相关内容