In [1]:
import toytree
import toyplot
import numpy as np
In [3]:
## create a random tree with N tips
tre = toytree.rtree.coaltree(24)
In [5]:
## assign random edge lengths and supports to each node
for node in tre.treenode.traverse():
node.dist = np.random.exponential(1)
node.support = int(np.random.uniform(50, 100))
In [6]:
## make a colormap
colormap = toyplot.color.brewer.map(name="Spectral", domain_min=0, domain_max=100)
In [7]:
## get node support values in plot order
vals = np.array(tre.get_node_values("support", show_root=1, show_tips=1))
vals
Out[7]:
In [16]:
## get colors mapped to values
colors = toyplot.color.broadcast((vals, colormap), shape=vals.shape)
colors = [toyplot.color.to_css(i) for i in colors]
Use None as the argument to get_node_values()
. This allows you to still toggle whether you want the root and tip nodes to show with the following arguments to get_node_values
. This argument would commonly be used if you wanted to set different node sizes or colors to represent values instead of having text show at nodes.
In [18]:
tre.draw(
height=400, width=300,
node_labels=tre.get_node_values(None, show_root=True, show_tips=False),
node_colors=colors,
node_sizes=10,
node_style={"stroke": "black"},
tip_labels_align=True,
);
Similar to above except now enter the name of a real node "feature" into the get_node_values
functions. This will return the values of the feature in node plotting order. The features "support", "dist", "name", "height", and "idx" are always available. Additional features can be added to nodes.
In [20]:
tre.draw(
height=400, width=350,
node_labels=tre.get_node_values("support", False, False),
node_colors=colors,
node_sizes=15,
node_style={"stroke": "black"},
use_edge_lengths=False,
);
In [21]:
tre.draw(
height=400, width=350,
use_edge_lengths=False,
node_labels=tre.get_node_values("support", False, False),
node_colors=colors,
node_sizes=0,
node_style={"stroke": "black"},
node_labels_style={
"baseline-shift": "8px",
"-toyplot-anchor-shift": "-15px",
"font-size": "10px"},
);