Tikz 树的位置问题

Tikz 树的位置问题

我的 Tikz 树出了点问题。连接第一层节点“NON Mammifero”的右边缘与边缘标签“NO”在同一层向下延伸。

我希望它在第一级停止,节点为“Partorisce”。我尝试使用同级距离和级别距离,但(我不知道为什么)不起作用。

提前致谢。

在此处输入图片描述

这里是图书馆:

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

这里是代码:

\begin{figure}[htbp]
\centering  
\begin{tikzpicture}[sibling distance=5cm,  level distance=3cm,
every node/.style = { 
    align=center,
    top color=white }]
\node[ellipse][draw]{Termofisiologia}
child { edge from parent node[above left,pos=.3]{Sangue-caldo} node[ellipse,draw] {Partorisce} 
    child { node[draw] {Mammifero}  edge from parent node[above left]{SI}} 
    child { node[draw] {NON Mammifero}  edge from parent node[above right]{NO}} }
child { node[draw] {NON Mammifero} edge from parent node[above right]{Sangue-freddo} };

\end{tikzpicture}
\vspace{0.2cm}
\caption{Esempio di albero decisionale}

答案1

看来你需要搬家了edge from parent node[..] {Sangue-caldo}搬到该节点的子节点Partorisce

代码输出

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{figure}[htbp]
\centering  
\begin{tikzpicture}[
  sibling distance=5cm,
  level distance=3cm,
  every node/.style = { 
    align=center,
    top color=white
  }
]
\node[ellipse][draw]{Termofisiologia}
  child { 
         node[ellipse,draw] {Partorisce} 
         child {
                node[draw] {Mammifero}
                edge from parent node[above left]{SI}
               } 
         child {
                node[draw] {NON Mammifero}
                edge from parent node[above right]{NO}
               }
         edge from parent node[above left] {Sangue-caldo} % this is moved to after the children of the node
         }
  child {
         node[draw] {NON Mammifero}
         edge from parent node[above right]{Sangue-freddo}
        };

\end{tikzpicture}
\caption{Esempio di albero decisionale}
\end{figure}
\end{document}

顺便说一下,这是forest替代方案,借用了 cfr 对在 Tikz 上绘制游戏树 forest是专为绘制树而设计的。

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{figure}[htbp]
\centering  

\begin{forest}
  for tree={
    l sep'+=25pt,
    s sep'+=5pt,
  },
  Rect/.style={
     draw,
     if={isodd(n)}%
        {edge label={node[auto,left,midway]{#1}}}%
        {edge label={node[auto,right,midway]{#1}}}
  },
  Ell/.style={
      Rect={#1},
      ellipse
  },
  Rect/.default={},
  Ell/.default={}
%
[Termofisiologia, Ell
  [Partorisce, Ell={Sangue-caldo}
    [Mammifero, Rect={NO}]
    [NON Mammifero, Rect={SI}]
  ]
  [NON Mammifero, Rect={Sangue-freddo}]
]
\end{forest}
\caption{Esempio di albero decisionale}
\end{figure}
\end{document}

答案2

这是使用另一种解决方案istgame包裹:

在此处输入图片描述

\documentclass{article}

\usepackage{istgame}

\begin{document}   

\begin{figure}[htbp]
\begin{istgame}%[font=\scriptsize]
\xtdistance{30mm}{50mm}
\istrooto(0){Termofisiologia}
  \istb{\mbox{Sangue-caldo}}[al]
  \istb{\mbox{Snague-freddo}}[ar]
  \endist
\istrooto(1)(0-1){Partorisce}
  \istb{\mbox{SI}}[al]
  \istb{\mbox{NO}}[ar]
  \endist
\istrooto(2)(0-2)[box node]{NON Mammifero}
  \endist
\istrooto(3)(1-1)[box node]{Mammifero}
  \endist
\istrooto(4)(1-2)[box node]{NON Mammifero}
  \endist
\end{istgame}
\caption{Esempio di albero descisionale}
\end{figure}

\end{document}

相关内容