绘制具有可变断点的水平树(图)

绘制具有可变断点的水平树(图)

我的部分工作涉及顺序乘积多项式及其分析。在论文或讲座中,使用我们所谓的“命运图”来描述底层概率结构通常很有用,该图通常是从左到右“阅读”(最左边的节点,个人抛硬币,可能是多项式,并根据抛硬币遵循路径。然后个人进入一个状态,获得一枚新硬币,抛硬币,依此类推……)。因此,图表(通常)从单个节点开始,然后快速分支。

到目前为止,我一直在使用专用的图形包在 *TeX 之外生成这些类型的图表,但我想看看是否有办法使用 *TeX 本身生成这些图表。我没有 MWE,但可以提供指向图形的指针,以说明我试图“绘制”的一个非常基本的示例:

在此处输入图片描述

挑战在于我经常需要的不对称“分支”(在右侧)。这类似于系统发育树,但我还无法弄清楚如何强迫我找到的任何包/方法给我我想要的东西。

任何建议都值得赞赏——即使是一个简单的代码示例,在 *TeX 中渲染“我的例子”也会很棒。Tikz、PSTricks、pgfplots——其实并不重要(我对任何一个都没有强烈的忠诚度)。

提前致谢...

答案1

您可能会喜欢元帖子对于这种类型的图表。我使用了盒子库来定义文本,然后无边界地绘制它们。

在此处输入图片描述

prologues := 3;
outputtemplate := "%j%c.eps";
input boxes;

verbatimtex
\font\sf=phvr8r at 10pt
\def\s#1{\vbox{\halign{\hss{\sf ##}\hss\cr#1\crcr}}}\let\\\cr
etex

beginfig(1);

boxit.A(btex \s{marked and\\released alive\\at start of year} etex);
boxit.S(btex \s{survives\\year} etex);
boxit.K(btex \s{killed by\\hunter} etex);
boxit.N(btex \s{dies from\\`natural' causes} etex);

boxit.r0(btex \s{not retrieved} etex);
boxit.r1(btex \s{retrieved but\\not reported} etex);
boxit.r2(btex \s{retrieved and\\reported} etex);

 K.c - A.c  = r2.c -  K.c = 140 right;
 K.c - N.c  =  S.c -  K.c =  60 up;
r2.c - r1.c = r0.c - r2.c =  42 up;

drawunboxed(A, S, K, N, r0, r1, r2);

path p[];
p1 = A.c -- S.c cutbefore bpath A cutafter bpath S;
p2 = A.c -- K.c cutbefore bpath A cutafter bpath K;
p3 = A.c -- N.c cutbefore bpath A cutafter bpath N;

p4 = K.c -- r0.w cutbefore bpath K;
p5 = K.c -- r1.w cutbefore bpath K; 
p6 = point .37 of p5 -- r2.w;

for $=1 upto 6: draw p[$]; endfor

label(btex $S$ etex         rotated angle direction 0.5 of p1, point .5 of p1 shifted 6 up);
label(btex $K$ etex         rotated angle direction 0.5 of p2, point .5 of p2 shifted 6 up);
label(btex $1-S-K$ etex     rotated angle direction 0.5 of p3, point .5 of p3 shifted 6 up);
label(btex $1-c$ etex       rotated angle direction 0.5 of p4, point .5 of p4 shifted 6 up);
label(btex $c$ etex         rotated angle direction 0.5 of p5, point .2 of p5 shifted 6 up);
label(btex $1-\lambda$ etex rotated angle direction 0.5 of p5, point .7 of p5 shifted 6 up);
label(btex $\lambda$ etex   rotated angle direction 0.5 of p6, point .5 of p6 shifted 6 up);

endfig;
end.

答案2

forest始终是一个选项:

命运森林

\documentclass[12pt, tikz]{standalone}
\usepackage{forest}
\begin{document}
\forestset{
  skip me/.style={
    shape=coordinate,
    inner sep=0pt,
    outer sep=0pt,
  },
  my label/.style n args=2{
      edge label={node [midway, sloped, #1, font=\scriptsize, inner sep=1pt] {#2}},
  },
}
\begin{forest}
  for tree={
    grow'=0,
    parent anchor=east,
    child anchor=west,
    anchor=west,
    align=center,
    if n children=3{
      for children={
        if n=2{
          calign with current edge,
        }{},
      },
    }{},
    tier/.wrap pgfmath arg={#1}{level()},
    l sep+=10mm
  }
  [marked and\\released alive\\at start of year
    [survives\\year, my label={above}{$S$}
    ]
    [killed by\\hunter, my label={above}{$K$}
      [not retrieved, tier=term, my label={above}{$1-c$}
      ]
      [, skip me, my label={below}{$c$}
        [retrieved and\\reported, my label={above}{$\lambda$}, tier=term
        ]
        [retrieved but\\not reported, my label={below}{$1-\lambda$}, tier=term
        ]
      ]
    ]
    [dies from\\natural causes, my label={below}{$1-S-K$}
    ]
  ]
\end{forest}
\end{document}

答案3

你可以适应像这样:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

%\begin{frame}
\begin{tikzpicture}[grow=right, sloped]

\node[bag] {$\bullet$}
    child {
        node[bag] {$tail$}        
            child {
                node[end, label=right:
                    $tail$] {} 
                edge from parent
                node[above] {}         
                node[below] {$\frac12$}
            }
            child {
                node[end, label=right:
                    $head$] {}
                edge from parent
                node[above] {$\frac12$}         
                node[below] {}
            }
        edge from parent 
            node[above] {}
            node[below] {$\frac12$}
    }
    child {
        node[bag] {$head$}        
            child {
                node[end, label=right:
                    $tail$] {}
                edge from parent
                node[above] {}         
                node[below] {$\frac12$}
            }
            child {
                node[end, label=right:
                    $head$] {}
                edge from parent
                node[above] {$\frac12$}         
                node[below] {}
            }
    edge from parent         
            node[above] {$\frac12$}
            node[below] {}
    };
\end{tikzpicture}
%

\end{document}

相关内容