我在决策树方面遇到了问题,三条边中的两条与父节点分离,我不明白为什么。我的论文中有很多树,而这棵树是唯一给我带来问题的树。(我的意思是它可能不是包错误)
提前致谢!
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
sibling distance=4cm,
level distance=2.5cm,
every node/.style = {
align=center,
top color=white
}
]
\node[ellipse][draw](Tipo di macchina){Tipo di macchina}
child {
node[draw, align=left] {C0:1\\C1:3}
edge from parent node(Tipo di macchina)[above left,pos=.6] {Monovolume}}
child {
node[draw, align=left] {C0:8\\C1:0}
edge from parent node(Tipo di macchina)[above right,pos=.6]{Sportiva}}
child {
node[draw, align=left] {C0:1\\C1:7}
edge from parent node(Tipo di macchina)[above right,pos=.2]{Lussuosa}};
\end{tikzpicture}
\vspace{0.2cm}
\caption{Esempio B}
\end{figure}
\end{document}
这是树的输出
答案1
问题是由于您为每个edge from parent
节点指定了相同的节点标签而引起的。如果您将这些标签设为不同的,则树将如您所愿:
事实上,除非您进一步引用图片中的这些节点,否则您根本不需要给它们贴上标签。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
sibling distance=4cm,
level distance=2.5cm,
every node/.style = {
align=center,
}
]
\node[ellipse,draw](Tipo di macchina){Tipo di macchina}
child {
node[draw, align=left] {C0:1\\C1:3}
edge from parent
node(Mono)[left,pos=.6] {Monovolume}
}
child {
node[draw, align=left] {C0:8\\C1:0}
edge from parent node(Sport)[above right,pos=.6]{Sportiva}
}
child {
node[draw, align=left] {C0:1\\C1:7}
edge from parent node(Lusso)[above right,pos=.2]{Lussuosa}
};
\end{tikzpicture}
\end{document}
答案2
另一种解决方案是使用istgame
包裹:
\documentclass[border=2pt]{standalone}
\usepackage{istgame}
\usepackage{makecell}
\begin{document}
\begin{istgame}
\xtdistance{25mm}{40mm}
\istrooto(0){Tipo di macchina}
\istb{\mbox{Monovolume}}[above left,near end]
\istb{\mbox{Sportiva}}[right]
\istb{\mbox{Lussuosa}}[above right,near start]
\endist
\xtNode(0-1)[box node]{\makecell{C0:1\\C1:3}}
\xtNode(0-2)[box node]{\makecell{C0:8\\C1:0}}
\xtNode(0-3)[box node]{\makecell{C0:1\\C1:7}}
\end{istgame}
\end{document}