我有一棵树,代码和结果如下所示。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\tikzset{
treenode/.style = {shape=rectangle, rounded corners,
draw, align=center},
root/.style = {treenode},
env/.style = {treenode},
leaf/.style = {shape=circle,draw,align=center, scale=0.5},
every node/.style = {font=\tiny}
}
\begin{document}
\begin{tikzpicture}
[
grow = down,
% sibling distance = 10em,
level 1/.style = {sibling distance=3.7cm},
level 2/.style = {sibling distance=1.8cm},
level 3/.style = {sibling distance=1.2cm},
level distance = 1cm,
edge from parent/.style = {draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
%sloped
]
\node [root] {R}
child { node [env] {I}
child{ node [env] {I}
child{ node [leaf] {L}
edge from parent node [ left] {LP}}
child{ node [leaf] {L}
edge from parent node [ right] {RP}}
edge from parent node [ left] {LP}
}
child{ node [env] {I}
child { node [env] {I}
child{ node [leaf] {L}
edge from parent node [ left] {LP}}
child{ node [leaf] {L}
edge from parent node [ right] {RP}}
edge from parent node [ left] {LP}}
child{ node [leaf] {L}
edge from parent node [ right] {RP}}
edge from parent node [ right] {RP}
}
edge from parent node [above right] {}
}
child { node [env] {I}
child { node [env] {I}
child{ node [] {$\ldots$}
edge from parent node [left] {LP}}
child{ node [] {$\ldots$}
edge from parent node [right] {RP}}
edge from parent node [left] {LP}
}
child{ node [leaf] {L}
edge from parent node [right] {RP}
}
edge from parent node [above left] {}
};
\draw [red] (-3.7,-4.2) rectangle +(3.6, 3.5) node[xshift=-3cm, yshift=-0.2cm] {up};
\draw [blue] (0.1,-3.2) rectangle +(2.9, 2.5) node[xshift=-2.3cm, yshift=-0.2cm] {down};
\end{tikzpicture}
\end{document}
连接圆角节点的边不接触节点。但圆形节点接触节点。边可以延伸到接触节点吗?
另外,我想在蓝色框的正下方添加图例。有什么好的方法可以在此位置添加图例吗?
答案1
您可以rounded rectangle
从shapes.misc
库中使用并调整inner ysep
:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary {shapes.misc}
\usepackage{pgfplots}
\tikzset{
treenode/.style = {shape=rounded rectangle, inner ysep=2pt,
draw, align=center},
root/.style = {treenode},
env/.style = {treenode},
leaf/.style = {shape=circle,draw,align=center, scale=0.5},
every node/.style = {font=\tiny}
}