Toytree: a minimalist Python tree plotting library

What is Toytree?

Toytree is a Python tree plotting library that uses the modern and minimalist graphics library Toyplot to generate high quality figures that can be displayed directly within jupyter-notebooks or exported in a variety of formats. Find the full documentation at


Toyplot can be easily installed using the conda software package manager with the command below.

In [1]:
## conda install toytree -c eaton-lab

To get started let's import a few modules

Import toytree, toyplot, and the numerical library numpy for generating data. If you installed toytree with conda then all of these modules will be available.

In [2]:
import toytree
import toyplot
import numpy
In [3]:

Loading/parsing trees

Toytree can parse a wide variety of tree formats in newick or extended newick format, where trees are represented as a set of nested parentheses with additional metadata linked to nodes.

In [3]:
## an example newick string with edge lengths and support values
newick = """

## create a toytree object from the newick string
tre = toytree.tree(newick)
In [4]:
## A toytree object in memory
<toytree.tree.Toytree at 0x7f0869e54050>

Simple tree plotting

To draw a tree from a Toytree object simply call its .draw() function which will generate a Canvas and a set of axes on which the plot is embedded. The Canvas is represented in HTML which allows it to be displayed seemlessly in a jupyter-notebook, and to look crisp on the web, as in this notebook presentation.

In [5]:

Interactive features

A fun advantage to generating figures in HTML is that you can easily add interactive features to your plots. In the example below I use the magic argument node_labels=True to diplay markers on nodes with an interactive hover feature. Hover your cursor over a node to see all of the features associated with it. Node features are parsed from the newick string, or can be added later.

In [6]:
tre.draw(width=200, node_labels=True);
appleorangetomatillopeppertomatoeggplantidx: 0 name: 0 dist: 0 support: 1idx: 1 name: 1 dist: 2 support: 100idx: 2 name: 2 dist: 1 support: 100idx: 3 name: 3 dist: 1 support: 90idx: 4 name: 4 dist: 2 support: 100idx: 5 name: apple dist: 2 support: 1idx: 6 name: orange dist: 4 support: 1idx: 7 name: tomatillo dist: 2 support: 1idx: 8 name: pepper dist: 3 support: 1idx: 9 name: tomato dist: 2 support: 1idx: 10 name: eggplant dist: 1 support: 1

Displaying node features

Besides the magic command node_values=True you can also set specific node features to be displayed on the tree. Or you can access node values to create custom lists to use as arguments to modify nodes colors or sizes. This is all controlled through arguments to the draw() function.

In [7]:
## get list of support values in node-order
supports = tre.get_node_values("support", show_root=False, show_tips=False)

## get list of node colors in node-order 
colors = [tre.colors[0] if i==100 else tre.colors[1] for i in supports]

## draw with node supports and some styling
    node_style={"stroke": "none"}

More on features and parsing trees

Toyplot uses a stripped-down version of the TreeNode object from the ete3 library to represent Tree objects in memory as a hierarchical structure that is easy to traverse and manipulate. This makes it easy to store and access meta-data on a tree in a programmatic way. See the ete documentation for many helpful tips on editing trees. In this example we add a new feature to each node and write the new tree string in extended newick format.

In [11]:
## a simple tree
tre = toytree.tree("(((a,b),c),d);")

## add a new feature to every node
for node in tre.tree.traverse():
    node.add_feature("new_feature", 100)
## write the tree string in NHX format with features
nhx = tre.tree.write(features=["new_feature", "dist", "support"], format=0)

## print the newick string

Viewing large trees

Viewing large trees is a bit easier with toytree than with alternative graphics libraries because the images can be easily viewed in a browser. Here we read in a large tree and display it. Scroll down to view the entire tree.

In [12]:
## load a newick string from a file path
newick = "/home/deren/Dropbox/Viburnum_t65_min4"
tre = toytree.tree(newick)

## draw it

Displaying cloudtrees

It can be informative to visualize a distribution of trees, such as a posterior distribution of species trees, and a cloud tree diagram which is easy to do with toyplot since opacity is an easy variable to modify. Here we create a MultiTree object which holds a list of Toytrees in memory, and call the draw_cloudtree() function.

In [19]:
## a file with trees sampled from a posterior dist.
mcmcfile = "/home/deren/Documents/ipyrad/tests/analysis-bpp/copy-d10-r0.mcmc.txt"

## use 'slice' operations to subsample every 10th tree from first 5000
mtrees = toytree.multitree(mcmcfile, treeslice=(0, 5000, 10))

## plot a cloud tree
        "opacity": 0.025, 
        "stroke": mtrees.colors[0]},
        "-toyplot-anchor-shift": "10px"},