我如何在 tikz 中创建这棵简单的无根树?

我如何在 tikz 中创建这棵简单的无根树?

我见过与我类似的问题,但我还没有找到或找到合适的解决方案——下面是一个简单的无根系统发育树。 在此处输入图片描述

这些数字与一个简单的距离矩阵相关,如下所示。 在此处输入图片描述

如何使用 tikz 创建这个?

我曾考虑过使用节点矩阵,但老实说,我很迷茫。

答案1

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
    
    \begin{tikzpicture}[leaf/.style={draw=white, thick, fill=black, circle}, inner/.style={draw=white, thick, fill=gray, circle}]
    
        \node[leaf,label={180:\textbf{\sffamily Chimp}}] (C) at (0,0) {};
    
        \node[leaf,label={180:\textbf{\sffamily Human}}] (H) at (0,-1.5) {};
        
        \node[leaf,label={0:\textbf{\sffamily Seal}},anchor=west] (S) at (4,0) {};
        
        \node[leaf,label={0:\textbf{\sffamily Whale}},anchor=west] (W) at (4,-1.5) {};
        
        \node[inner] (il) at (1.3,-.75) {};
        \node[inner] (ir) at (2.8,-.75) {};
        
        \foreach \from/\to/\opt/\lab in 
        {C/il/above/1,
        H/il/below/2,
        il/ir/above/3,
        ir/S/above/2,
        ir/W/below/0
        }{
            \path[thick,black] (\from) edge [\opt] node {\lab} (\to);
        }
    \end{tikzpicture}
\end{document}

生产

例子

答案2

使用包绘制树forest

\documentclass[margin=3.14159mm]{standalone}
\usepackage{forest}
\tikzset{ELS/.style = {% Edge Label Style
              pos=0.5, sloped, node font=\footnotesize, text=blue,
              inner sep=2pt},
        }

\begin{document}
   \begin{forest}
for tree = {
% node style
    circle,
    draw=gray!50, very thick,
    fill=black,
    minimum size=4mm,
tikz/every label/.style = {font=\sffamily\bfseries},
% tree style
    edge = {semithick},
    l sep=6mm,
    s sep=7mm,
EL/.style={if n=1{edge label={node[ELS, anchor=south]{$#1$}}}
                 {edge label={node[ELS, anchor=north]{$#1$}}}}
           }
% tree body code
[,coordinate, label={[ELS]3}
    [ , fill=gray,
        %%%%    settings for the right branch
        for tree={grow'=east}, 
        before computing xy={l=0, s=+7mm},
        %%%%
       [,label=right:Seal,  EL=2]
       [,label=right:Whale, EL=0]
    ]
    [ , fill=gray,
        %%%%    settings for the left branch
        for tree={grow=west},
        before computing xy={l=0, s=-7mm},
        %%%%
        [,label=left:Chimp,  EL=1]
        [,label=left:Human, EL=2]
    ]
]
    \end{forest}
\end{document}

在此处输入图片描述

相关内容