如何旋转决策树

如何旋转决策树

我想画一个水平决策树。有人能帮我修改代码吗?

我是新来的。如果我的问题不清楚,请告诉我 :)

多谢!

在此处输入图片描述

\documentclass[11pt]{article}
\usepackage{forest}

\forestset{
     declare toks={elo}{}, % Edge Label Options
     anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
     dot/.style={tikz+={\fill (.child anchor) circle[radius=#1];}},
     dot/.default=2pt,
     decision edge label/.style n args=3{
     edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut$\unexpanded{#3}$}}
     },
     decision/.style={if n=1
     {decision edge label={left}{east}{#1}}
     {decision edge label={right}{west}{#1}}
     },
     decision tree/.style={
     for tree={
     s sep=0.5em,l=8ex,
     if n children=0{anchors=north}{
     if n=1{anchors=south east}{anchors=south west}},
     math content,
     },
     anchors=south, outer sep=2pt,
     dot=3pt,for descendants=dot,
     delay={for descendants={split option={content}{;}{content,decision}}},
     }
    }

\begin{document}

\begin{forest} decision tree
     [N,plain content
     [I;{p_1=0.5},plain content,elo={yshift=4pt}
     [{5,1};a]
     [II;b,plain content
     [{1,2};m]
     [{2,3};n]
     ]
     ]
     [II;{p_2=0.5},plain content,elo={yshift=4pt}
     [;c
     [{1,0};z]
     [{2,2};t]
     ]
     [;d
     [{3,1};z]
     [{0,0};t]
     ]
     ] {\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,above]{I};}
     ]
    \end{forest}

\end{document} 

答案1

不幸的是,我对的知识forest太薄弱,无法处理您的 MWE。因此,作为如何绘制水平决策树的起点,让我展示更简单的forest树代码:

\documentclass[margin=3mm]{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
/tikz/every label/.append style={inner sep=1pt,font=\footnotesize},
  for tree={
    grow'=east,       % <---
  % node style
    circle,
    minimum size=3pt,
    inner sep=0pt,
    outer sep=0pt,
    if n children=0{}{fill,fit=band},  
  % distances of nodes
      l sep=13mm,           
      s sep=0mm,            
  % edges
    edge={draw},
    edge path'={(!u.parent anchor) -- (.child anchor)},
    tier/.option=level,
  % labels on edges
   delay={edge label/.wrap value={node[midway, above, sloped, inner sep=1pt,
                                       font=\scriptsize]{#1}}
          },  % <---  
  }, % end for tree
[ , label=west:$N$
    [ , label=II, edge label={$p=0.5$}
        [ , edge label=$d$
           [{0,0}, edge label={$t$}]
           [{3,1}, edge label={$z$}]
        ]
        [ , edge label={$c$}
          [{2,2}, edge label={$t$}]
          [{1,0}, edge label={$z$}]
        ]
    ]{\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,label=left:I] {};}
    [ , label=I, edge label={$p=0.5$}
        [ , label=II, edge label={$b$}
          [{2,3}, edge label={$n$}]
          [{1,2}, edge label={$m$}]
        ]
        [{5,1}, edge label=$a$]
    ]
]
\end{forest}
\end{document}

这使:

在此处输入图片描述

我希望,将我的树代码扩展到您的复杂解决方案不会给您带来太多麻烦。在最初的空闲时间里,我将尝试弄清楚您的复杂 MWE。


\documentclass[11pt]{article}
\usepackage{forest}

\forestset{
     declare toks={elo}{}, % Edge Label Options
     anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
     dot/.style={tikz+={\fill (.child anchor) circle[radius=#1];}},
     dot/.default=2pt,
     decision edge label/.style n args=3{
     edge label/.expanded={node[midway,#1=-2pt,anchor=#2,\forestoption{elo}]{\strut$\unexpanded{#3}$}}
     },
     decision/.style={if n=1
     {decision edge label={above left}{south east}{#1}}
     {decision edge label={below left}{north east}{#1}}
     },
     decision tree/.style={
     for tree={grow'=east,
     s sep=0.5em,l=8ex,
     if n children=0{anchors=west}{
     if n=1{anchors=south}{anchors=north}},
     math content,
     },
     anchors=south, outer sep=2pt,
     dot=3pt,for descendants=dot,
     delay={for descendants={split option={content}{;}{content,decision}}},
     }
    }

\begin{document}

\begin{forest} decision tree
     [N,plain content
     [I;{p_1=0.5},plain content,elo={yshift=4pt}
     [{5,1};a]
     [II;b,plain content
     [{1,2};m]
     [{2,3};n]
     ]
     ]
     [II;{p_2=0.5},plain content,elo={yshift=4pt}
     [;c
     [{1,0};z]
     [{2,2};t]
     ]
     [;d
     [{3,1};z]
     [{0,0};t]
     ]
     ] {\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,left]{I};}
     ]
    \end{forest}

\end{document} 

在此处输入图片描述

相关内容