绘制概率树

绘制概率树

我正在尝试绘制一个概率树,但我重叠了两个节点,在右侧,我应该最终得到 4 个节点,它们与空节点的距离相同,一个在另一个之上。但正如我之前提到的,我重叠了其中 2 个,只留下 3 个可见。如果有人知道我该如何解决它,我将不胜感激,在此先谢谢您!

\begin{tikzpicture}[grow=right,sibling distance=8em, level distance=3cm,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=blue!20}]]
  \node {}
    child { node {$A^c$}
      child { node {$B^c$}
        edge from parent node[below] { $P(B^c|A^c)$ } }
      child { node {$B$}
        edge from parent node[above] { $P(B|A^c)$ } }
      edge from parent node[above] { $P(A^c)$ } }
    child { node {$A$}
      child { node {$B^c$}
        edge from parent node[below] { $P(B^c|A)$ } }
      child { node {$B$}
        edge from parent node[above] { $P(A)$ } }
      edge from parent node[above] { $P(B|A)$ } };
\end{tikzpicture}

答案1

您需要为树的每一层定义不同的兄弟距离。例如:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
grow=right,
level distance=3cm,
level 1/.style = {sibling distance=12em},
level 2/.style = {sibling distance=8em},
%
every node/.style = {shape=rectangle, rounded corners,
                     draw, align=center,
                     top color=white, bottom color=blue!20}
                    ]
\node {}
    child { node {$A^c$}
      child { node {$B^c$}
        edge from parent node[below] { $P(B^c|A^c)$ } }
      child { node {$B$}
        edge from parent node[above] { $P(B|A^c)$ } }
      edge from parent node[above] { $P(A^c)$ } }
    child { node {$A$}
      child { node {$B^c$}
        edge from parent node[below] { $P(B^c|A)$ } }
      child { node {$B$}
        edge from parent node[above] { $P(A)$ } }
      edge from parent node[above] { $P(B|A)$ } };
\end{tikzpicture}
\end{document}

在此处输入图片描述

附录:
决策树的一个版本,借助软件包绘制forest,边缘标签较少。但是它们与边缘对齐,并自动定位在边缘上方/下方:

\documentclass[margin=3mm]{standalone}
\usepackage{forest}

\begin{document}
    \begin{forest}
for tree={
% nodes
    draw, rounded corners=2pt,
  where level=0{circle}{minimum width=2em},
    top color=white, bottom color=blue!20,
math content,
% tree
    grow'=east,
     edge=semithick,
   child anchor=west,
    l sep=22mm,
    s sep=6mm,
    where level=1{s sep=2mm}{},% insert different `s sep`
tier/.option = level,
/tikz/ELS/.style = {% Edge Label Style
                    pos=0.5, sloped, node font=\footnotesize,
                    inner sep=2pt, anchor=#1},
EL/.style = {if n=1{% Edge Label, automatic positioned
                     edge label={node[ELS=south]{$#1$}}}
                    {edge label={node[ELS=north]{$#1$}}}}
        },
[
    [A,     EL=P(B|A)
        [B,     EL=P(A)]
        [B^c,   EL=P(B^c|A)]
    ]
    [A^c,   EL=P(A^c)
        [B,     EL=P(B|A^c)]
        [B^c,   EL=P(B^c|A^c)]
    ]
]
    \end{forest}
\end{document}

在此处输入图片描述

答案2

语法child相当陈旧,不太先进。在这个简单的情况下,您可以设置不同的sibling distances,但通常您希望采用更高级的解决方案,例如forest包。

另一个选择是需要 LuaLaTeX 的graphsdrawing库(及其模块)。使用该库及其语法,指定树的元素还可以减少输入。treesgraphs

代码

% !TeX TS-program = lualatex
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs, graphdrawing, quotes}
\usegdlibrary{trees}
\tikzset{math node/.style={execute at begin node=$, execute at end node=$}}
\begin{document}
\tikz\graph[
  tree layout, grow' = 0, level distance = 2cm,
  fresh nodes, math nodes,
  nodes = {
    shape = rectangle, rounded corners,
    draw, align = center,
    top color = white, bottom color = blue!20},
  edge quotes = {sloped, auto, node font = \footnotesize, math node},
  ]{
    root[as =, rounded corners = +.3333em] -- {
      A   [> "P(B|A)"] -- {
        B   [> "P(      A)" ],
        B^c [> "P(B^c | A)"']
      },
      A^c [> "P(A^c)"'] -- {
        B   [> "P(B  |A^c)" ],
        B^c [> "P(B^c|A^c)"']
      }
    }
  };
\end{document}

输出

在此处输入图片描述

相关内容