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
In [3]:
import toytree
In [4]:
# generate a random tree
tree = toytree.rtree.unittree(ntips=10, seed=123)
In [5]:
tree.draw(ts='n');
In [7]:
tree.draw(ts='s');
In [8]:
tree.draw(ts='c');
In [9]:
tree.draw(ts='o');
In [10]:
tree.draw(ts='p');
In [11]:
tree.draw(ts='d');
In [17]:
tree.draw(ts='c', tip_labels=True, node_colors='orange');
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.
In [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();
In [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();
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.
In [32]:
# applying a tree style will override any .style modifications
stree.draw(ts='c');
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.
In [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');