我想畫一個水平決策樹。誰能幫我修改我的程式碼嗎?
我是新來的。因此,如果我的問題不清楚,請告訴我:)
多謝!
\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}