Using built-in Tree Styles

The built-in treestyle or ts drawing options in toytree provide a base layer for styling drawings that can make it easier to achieve a desired style using fewer options. Additional styles can be applied on top of these base layers to extend styling.

The currently supported types are:

n: normal – default style
s: simple – clean focus on topology and node labels
c: coalescent – clean focus on topology and timing
o: umlaut – just a nice looking style
p: population – species/population trees, auto-parses “Ne”
d: dark – light colors
[3]:
import toytree
[4]:
# generate a random tree
tree = toytree.rtree.unittree(ntips=10, seed=123)

Examples

[5]:
tree.draw(ts='n');
r0r1r2r3r4r5r6r7r8r9
[7]:
tree.draw(ts='s');
0123456789101112131415161718r0r1r2r3r4r5r6r7r8r9
[8]:
tree.draw(ts='c');
0.00.51.0
[9]:
tree.draw(ts='o');
r0r1r2r3r4r5r6r7r8r9
[10]:
tree.draw(ts='p');
0123456789101112131415161718r0r1r2r3r4r5r6r7r8r90.00.51.0
[11]:
tree.draw(ts='d');
r0r1r2r3r4r5r6r7r8r9

Extend styling

Additional styles can be applied on top of a ts style to extend styling. In this sense the treestyle should be thought of as changing the default base layer of style before additional drawing options are parsed and applied.

[17]:
tree.draw(ts='c', tip_labels=True, node_colors='orange');
r0r1r2r3r4r5r6r7r8r90.00.51.0

Tree style overrides style applied to toytree objects

In addition to providing style options to the .draw() function, it is also possible to set a tree’s style before calling draw by modify a tree’s .style attribute. This is effectively a way to modify its default base style. This can be useful if, for example, you want to apply different styles to a bunch of trees that will be plotted in a multitree drawing. In general, though, I recommend just styling drawings by using options in the .draw() function.

This is relevant to mention here because if you use the treestyle argument in .draw it overrides the default style of the tree. This means it will also override any changes that have been made to the .style attribute of a tree. This is demonstrated below.

[25]:
# generate a random tree
stree = toytree.rtree.unittree(ntips=10, seed=4321)

# modify some styles of a tree
stree.style.use_edge_lengths = False
stree.style.scalebar = True
stree.style.edge_type = 'b'
stree.style.layout = 'd'

# draw the tree
stree.draw();
r0r1r2r3r4r5r6r7r8r9024
[31]:
# get 5 random trees
trees = [toytree.rtree.bdtree(8) for i in range(5)]

# set the edge_colors style on each tree
for tre in trees:
    tre.style.edge_colors = next(toytree.icolors1)

# draw multiple trees using their applied styles
toytree.mtree(trees).draw();
r0r1r2r3r4r5r6r7r0r1r2r3r4r5r6r7r0r1r2r3r4r5r6r7r0r1r2r3r4r5r6r7

This is the same tree object as above that has styles applied to it but when a treestyle is applied it erases the applied styles.

[32]:
# applying a tree style will override any .style modifications
stree.draw(ts='c');
0.00.51.0

Here you can see that we set colors on the edges just like above, but when a treestyle is applied it erases the applied styles.

[34]:
# get 5 random trees
trees = [toytree.rtree.bdtree(8) for i in range(5)]

# set the edge_colors style on each tree
for tre in trees:
    tre.style.edge_colors = next(toytree.icolors1)

# draw multiple trees using their applied styles
toytree.mtree(trees).draw(ts='n');
r0r1r2r3r4r5r6r7r0r1r2r3r4r5r6r7r0r1r2r3r4r5r6r7r0r1r2r3r4r5r6r7