Node labels are markers plotted on the nodes (vertices) of a tree and can be highly modified, including the addition of interactive features which are useful not only for producing final production-quality figures but also for exploring metadata on trees.
In [1]:
import toytree
import toyplot
import numpy as np
In [8]:
# newick tree string with edge lengths and support values
newick = """
((apple:2,orange:4)100:2,(((tomato:2,eggplant:1)100:2,pepper:3)90:1,tomatillo:2)100:1);
"""
# load toytree
tre = toytree.tree(newick)
The argument node_labels
can be entered as a list of names or left as None or False in which case the node labels are not shown. If you enter True a special feature is applied to the nodes in which case they will become interactive and show information for all available features (node values stored in the Tree object metadata) in the Tree. Hover your cursor over the green node labels below for an example.
In [9]:
## no node labels
tre.draw();
## default node labels
tre.draw(node_labels=True);
The order in which nodes and node values are plotted can be accessed from toytrees directly using .get_node_values()
. By linking this function to the tree itself we can be sure that the node labels will be returned in the correct order regardless of whether the tree has been pruned, or rotated, or any other modification.
"feature" argument that is provided. All trees will always have the features ["idx", "name", "support", "dist", "height"]
available. Below we grab the 'height' values and plot them on the nodes of the tree. The .get_node_values()
function does not return values for the root node or for leaves by default, since these often are not meaningful on trees where the values represent support values or distances (i.e, the values are actually relevant to edges not nodes), but we want those values here, so we enter True for those arguments.
In [10]:
## get node index (idx) values
heights = tre.get_node_values("height", show_root=True, show_tips=False)
## plot tree with node values
tre.draw(node_labels=heights, tip_labels_align=True, orient='down');
In [12]:
## plot tree with node values
tre.draw(
node_labels=tre.get_node_values("support"),
node_sizes=24,
node_labels_style={"font-size": "10px"},
);
In [14]:
## traverse tree and add a new feature to each node
for node in tre.treenode.traverse():
if node.dist > 1:
node.add_feature("is_long_branch", True)
else:
node.add_feature("is_long_branch", False)
In [16]:
## get new feature in node plot order
feature = tre.get_node_values("is_long_branch", 0, 0)
## plot tree with node values
tre.draw(
width=350,
node_labels=feature,
node_sizes=25,
use_edge_lengths=True,
node_labels_style={"font-size": "9px"},
);
Here we apply different colors to each node based on its value for the feature that we assigned to the nodes above, is_long_branch. We also use the magic argument node_labels=True
which assigns interactive information to each node. Hover your cursor over the nodes in the plot below to see how the colors match up with the features of the node.
In [18]:
## get new feature in node plot order
feature = tre.get_node_values("is_long_branch", 0, 0)
colors = ["orange" if i else "lightgrey" for i in feature]
## plot tree with node values
tre.draw(
width=300,
node_labels=None,
node_sizes=12,
node_colors=colors,
node_style={"stroke":"black"},
);
In [20]:
## get new feature in node plot order
feature = tre.get_node_values("is_long_branch", 0, 0)
colors = ["white" if i else "lightgrey" for i in feature]
## plot tree with node values
tre.draw(
node_labels=True,
node_sizes=20,
node_colors=colors,
node_style={"stroke": "darkcyan", "stroke-width": "2px"},
);
In [ ]: