TikZ 樹

TikZ 樹

很久以前,我寫了一個機率樹的程式碼(見下圖),不幸的是我遺失了程式碼。有人有想法嗎,我怎麼能再實現它?

我已經嘗試過舊版本但失敗了

\begin{figure}[H]
\centering
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=0.6cm,y=0.5cm]
\clip(0,-6.5) rectangle (18,6);
 \draw [line width=2pt] (2,-1)-- (9,2);
 \draw [line width=2pt] (2,-1)-- (9,-4);
  \draw [line width=2pt] (9,2)-- (16,4);
  \draw [line width=2pt] (9,2)-- (16,0);
  \draw [line width=2pt] (9,-4)-- (16,-2);
   \draw [line width=2pt] (9,-4)-- (16,-6);
  \filldraw [red] (2,-1) circle (2.8pt);
  \filldraw [red] (9,2) circle (2.8pt);
  \filldraw [red] (9,-4) circle (2.8pt);
  \filldraw [red] (16,-6) circle (2.8pt);
   \filldraw [red] (16,-2) circle (2.8pt);
  \filldraw [red] (16,4) circle (2.8pt);
 \filldraw [red] (16,0) circle (2.8pt);
 \node (A) at (9,2.8) {$V$};
 \node (B) at (9,-3.2) {$\overline{V}$};
 \node (C) at (16,4.8) {$S$};
 \node (D) at (16,0.8) {$\overline{S}$};
 \node (E) at (16,-1.2) {$S$};
 \node (F) at (16,-5.2) {$\overline{S}$}; 
\node (G) at (5,1.5) {\scriptsize {\color{blue}$P(V)=0,4$}};
\node (G) at (5,-3.5) {\scriptsize {\color{blue}$P(\overline{V})=0,6$}};
\node (G) at (12,3.8) {\scriptsize {\color{blue}$P(S|V)=0,6$}};
\node (G) at (12,0) {\scriptsize {\color{blue}$P(\overline{S}|V)=0,4$}};
\node (G) at (12,-2) {\scriptsize {\color{blue}$P(S|\overline{V})=0,3$}};
\node (G) at (12,-6) {\scriptsize {\color{blue}$P(\overline{S}|\overline{V})=0,7$}};
\end{tikzpicture}
\caption{Baumdiagramm für Aufgabe 11}

\end{figure}  

機率樹

答案1

簡而言之,要修改您的程式碼,請替換您擁有的所有實例

\filldraw [red] (x,y) circle (2.8pt);

\node [draw=black,fill=red!10,circle] at (x,y) {$A$};

除了第一個紅色圓圈之外,您可以將其刪除。當然,請替換$A$為相關節點的適當符號。這是您的程式碼的一個版本,我對其進行了一些清理。我沒有更改節點中的文本,但您可以管理。

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,x=0.6cm,y=0.5cm]
\coordinate (O) at (2,-1);
\begin{scope}[every node/.style={circle,draw=black,fill=red!10}]
 \node (A) at (9,2) {$V$};
 \node (B) at (9,-4) {$\overline{V}$};
 \node (C) at (16,4) {$S$};
 \node (D) at (16,0) {$\overline{S}$};
 \node (E) at (16,-2) {$S$};
 \node (F) at (16,-6) {$\overline{S}$}; 
\end{scope}

\draw (O) -- (A) -- (C)
             (A) -- (D)
      (O) -- (B) -- (E)
             (B) -- (F);

\begin{scope}[every node/.style={blue,font=\scriptsize}]
\node at (5,1.5) {$P(V)=0,4$};
\node at (5,-3.5) {$P(\overline{V})=0,6$};
\node at (12,3.8) {$P(S|V)=0,6$};
\node at (12,0) {$P(\overline{S}|V)=0,4$};
\node at (12,-2) {$P(S|\overline{V})=0,3$};
\node at (12,-6) {$P(\overline{S}|\overline{V})=0,7$};
\end{scope}
\end{tikzpicture}
\end{document}

在此輸入影像描述

TikZ 樹

然而,這是一棵樹,所以你也可以使用 TikZ 的樹繪製功能,如下所示:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
   circ/.style={draw=black,fill=red!15,circle},
   grow=right,
   level 1/.style={sibling distance=2cm},
   level 2/.style={sibling distance=1cm},
   level distance=3cm]
\node [coordinate] {}
  child {
    node[circ] {$\bar{A}$}
      child { node[circ] {$\bar{D}$}
        edge from parent
        node [below=2] {$0.9999$}}
      child { node[circ] {$D$}
        edge from parent
        node [above=2] {$0.0001$}}
    edge from parent
    node [below=2] {$0.9999$}
    }
  child {
    node[circ] {$A$}
      child { node[circ] {$\bar{D}$}
        edge from parent
        node [below=2] {$0.0001$}}
      child { node[circ] {$D$}
        edge from parent
        node [above=2] {$0.9999$}
  }
   edge from parent
   node [above=2] {$0.0001$}
  };
\end{tikzpicture}
\end{document}

在此輸入影像描述

forest

或者使用更強大的forest軟體包,我認為這是相當流行的。不過我不是專家,所以很可能可以更優雅地解決邊緣標籤。

\documentclass[border=5mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
  math content,
  if n=0{coordinate}{circle,draw=black,fill=red!10},
  grow=0,
  l=3cm
}
[
 [\overline{A},edge label={node[midway,below=2pt]{0.9999}}
  [\overline{D},edge label={node[midway,below]{0.9999}}]
  [D,edge label={node[midway,above]{0.0001}}] 
 ]
 [A,edge label={node[midway,above=2pt]{0.0001}}
  [\overline{D},edge label={node[midway,below]{0.0001}}]
  [D,edge label={node[midway,above]{0.9999}}] 
 ]
]
\end{forest}
\end{document}

在此輸入影像描述

答案2

一個小的變化森林案例中托比昂 T.答:

\documentclass[border=5mm]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}% <-- added

\begin{document}
\forestset{% <-- added
  EL/.style 2 args={% shortens for (my) edge label
    edge label={node[midway, font=\footnotesize, #1]{#2}},
                    },
        }% end of forestset

\begin{forest}
for tree={
  math content, % <-- all text is math
  if n=0{coordinate}{circle,draw=black,fill=red!10,drop shadow},% <-- slightly changed
  grow=0,
  l sep = 24mm,
        }% end of for tree
[
    [\overline{A},EL={below=1pt}{0.9999}% <-- changed
        [\overline{D},EL={below}{0.9999}]
        [D,EL={above}{0.0001}]
    ]
    [A,EL={above=2pt}{0.0001}
        [\overline{D},EL={below}{0.0001}]
        [D,EL={above}{0.9999}]
    ]
]
\end{forest}
\end{document}

差異在於邊緣標籤的書寫。結果類似:

在此輸入影像描述

(使用標籤書寫的想法取自CFR回答這裡.)

相關內容