我正在努力理解如何將節點放在伯努利試驗樹的第一層的分支上。當我嘗試將其繪製出來時,分支中心的節點標籤成為我的子節點的起點。有人可以提供一些建議嗎?
謝謝!
\documentclass{article}
\usepackage[margin = 1in]{geometry}
\usepackage{amsfonts}
\usepackage{amsthm, amsmath, amssymb}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
[font=\footnotesize,
grow=right, level 1/.style={sibling distance=6em},
level 2/.style={sibling distance=1em}, level distance=5cm]
\node (Root) {} % root
child { node {s} edge from parent node [below = .25cm] {$\frac{1}{2}$}
child { node {s} edge from parent node [above = .25cm] {$\frac{1}{2}$}}
child { node {f} edge from parent node [below = .25cm] {$\frac{1}{2}$}}
}
child { node {f}
child { node {s} edge from parent node [above = .25cm] {$\frac{1}{2}$}}
child { node {f} edge from parent node [below = .25cm] {$\frac{1}{2}$}}
};
\end{tikzpicture}
\end{document}
答案1
在 TikZ 樹中,要修改從父節點(例如P
)到其子節點(例如C1
)的分支(例如更改其顏色、添加文字和/或其他樣式),您必須將鍵放在所有子節點和孫子節點edge from parent
之後C1
的C1
。
程式碼
\documentclass{article}
\usepackage[margin = 1in]{geometry}
\usepackage{amsfonts}
\usepackage{amsthm, amsmath, amssymb}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
[font=\footnotesize,
grow=right, level 1/.style={sibling distance=6em},
level 2/.style={sibling distance=1em}, level distance=5cm]
\node (Root) {} % root
child { node {s}
child { node {s} edge from parent node [above = .25cm] {$\frac{1}{2}$}}
child { node {f} edge from parent node [below = .25cm] {$\frac{1}{2}$}}
edge from parent node [below = .25cm] {$\frac{1}{2}$} % NEW
}
child { node {f}
child { node {s} edge from parent node [above = .25cm] {$\frac{1}{2}$}}
child { node {f} edge from parent node [below = .25cm] {$\frac{1}{2}$}}
edge from parent node [above = .25cm] {$\frac{1}{2}$} % NEW
};
\end{tikzpicture}
\end{document}