我有以下决策树这个例子稍作修改。当我向第二个右侧节点添加两个子节点时,内部左侧节点和内部右侧节点已经重叠。我该如何分离这些节点?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newdimen\nodeDist
\nodeDist=35mm
\begin{document}
\begin{tikzpicture}[
node/.style={%
draw,
rectangle,
},
]
\node [node] (A) {SimilarityScore1};
\path (A) ++(-135:\nodeDist) node [node] (B){loooooongeteeext};
\path (A) ++(-45:\nodeDist) node [node] (C) {loooooongeteeext};
\path (C) ++(-135:\nodeDist) node [node] (D) {loooooongeteeext};
\path (C) ++(-45:\nodeDist) node [node] (E) {loooooongeteeext};
\path (B) ++(-135:\nodeDist) node [node] (F) {loooooongeteeext};
\path (B) ++(-45:\nodeDist) node [node] (G) {loooooongeteeext};
\draw (A) -- (B) node [left,pos=0.25] {$<= 0.904712$}(A);
\draw (A) -- (C) node [right,pos=0.25] {$> 0.904712$}(A);
\draw (C) -- (D) node [left,pos=0.25] {$<= 0.896089$}(A);
\draw (C) -- (E) node [right,pos=0.20] {$> 0.896089$}(A);
\draw (B) -- (F) node [left,pos=0.2] {$<= 0.997372$}(A);
\draw (B) -- (G) node [right,pos=0.20] {$> 0.997372$}(A);
\end{tikzpicture}
\end{document}
提前致谢
答案1
您必须更改连接器的长度或角度。或者两者兼而有之,这里有一个建议,可以同时实现我认为合理的目标:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newdimen\nodeDist
\nodeDist=25mm
\newdimen\nodeDistUpper
\nodeDistUpper=40mm
\begin{document}
\begin{tikzpicture}[
node/.style={%
draw,
rectangle,
},
]
\node [node] (A) {SimilarityScore1};
\path (A) ++(-157.5:\nodeDistUpper) node [node] (B){loooooongeteeext};
\path (A) ++(-22.5:\nodeDistUpper) node [node] (C) {loooooongeteeext};
\path (C) ++(-135:\nodeDist) node [node] (D) {loooooongeteeext};
\path (C) ++(-45:\nodeDist) node [node] (E) {loooooongeteeext};
\path (B) ++(-135:\nodeDist) node [node] (F) {loooooongeteeext};
\path (B) ++(-45:\nodeDist) node [node] (G) {loooooongeteeext};
\draw (A) -- (B) node [left,pos=0.25] {$\leq 0.904712$}(A);
\draw (A) -- (C) node [right,pos=0.25] {$> 0.904712$}(A);
\draw (C) -- (D) node [left,pos=0.25] {$\geq 0.896089$}(A);
\draw (C) -- (E) node [right,pos=0.20] {$> 0.896089$}(A);
\draw (B) -- (F) node [left,pos=0.2] {$\leq 0.997372$}(A);
\draw (B) -- (G) node [right,pos=0.20] {$> 0.997372$}(A);
\end{tikzpicture}
\end{document}
答案2
另一种不同的方法。由于您的图表是树形(决策)图,因此可以借助forest
包轻松绘制:
\documentclass[border=5mm]{standalone}
\usepackage{forest}
\begin{document}
\forestset{EL/.style 2 args={edge label={%
node[midway, font=\footnotesize,
inner sep=2pt, anchor=south #1]{$#2$}},
},
}% end of forestset
\begin{forest}
for tree={
draw,
l sep=17 mm,
s sep= 1 mm,
}% end of for tree
[SimilarityScore1
[loooooongeteeext, EL={east}{\leq 0.904712},
[loooooongeteeext,EL={east}{\leq 0.896089}]
[loooooongeteeext,EL={west}{\geq 0.896089}]
]
[loooooongeteeext, EL={west}{\geq 0.904712}
[loooooongeteeext,EL={east}{\leq 0.896089}]
[loooooongeteeext,EL={west}{\geq 0.896089}]
]
]
\end{forest}
\end{document}
附录: 根据答案我的问题可以稍微改进一下(使树的代码更短)姆韦:
\documentclass[border=3mm, preview]{standalone}
\usepackage{forest}
\begin{document}
\tikzset{ELS/.style={% Edge Label Style
font=\footnotesize\sffamily, inner sep=2pt,
anchor=south #1, % label position: "ELS=west" or "EL=east"
pos=0.6}
}
\begin{forest}
for tree={
draw,
semithick,
parent anchor=children,
l sep=13 mm,
s sep=4mm,
where level=1{s sep=1mm}{} % <---
},
EL/.style = {% <---
before typesetting nodes={%
where n=1{%
edge label/.wrap value={node[ELS=east]{$#1$}}
}{%
edge label/.wrap value={node[ELS=west]{$#1$}}
}
}
}
[SimilarityScore1
[loooooongeteeext, EL=\leq 0.904712
[loooooongeteeext,EL=\leq 0.896089]
[loooooongeteeext,EL=\geq 0.896089]
]
[loooooongeteeext, EL=\geq 0.904712
[loooooongeteeext,EL=\leq 0.896089]
[loooooongeteeext,EL=\geq 0.896089]
]
]
\end{forest}
\end{document}
这给出了更好的结果:
答案3
一个完全不同的方法可以让你让整个方法更加通用,就是这样做。如果你有多棵树,而且它们并不都拥有完全相同的结构,那么这也会对你有所帮助
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[-,thick,
level 1/.style={sibling distance=6cm},
level 2/.style={sibling distance=2.5cm}
]
\footnotesize
\node {similarity score}
child {node {looooong text 1}
child {node {looooong text 1a}}
child {node {looooong text 1b}}
}
child {node {looooong text 2}
child {node {looooong text 2a}}
child {node {looooong text 2b}}
}
;
\end{tikzpicture}
\end{document}
您还可以添加方框和标签
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[-,thick,
level 1/.style={sibling distance=6cm},
level 2/.style={sibling distance=2.5cm},
box/.style={draw=blue!80}
]
\footnotesize
\node[box] {similarity score}
child {node[box] {looooong text 1}
child {node[box] {looooong text 1a} edge from parent node[left] {$<3$} }
child {node[box] {looooong text 1b} edge from parent node[right] {$\geq3$} }
edge from parent node[left] {$<3$}
}
child {node[box] {looooong text 2}
child {node[box] {looooong text 2a} edge from parent node[left] {$<3$}}
child {node[box] {looooong text 2b} edge from parent node[right] {$\geq3$}}
edge from parent node[right] {$\geq3$}
}
;
\end{tikzpicture}
\end{document}