Styling toytree drawings

The number of styling available in toytree is enormous and will continue to grow as development on the project continues. If you have a specific styling option that does not appear to be supported please raise a issue on GitHub and we can discuss adding support for it. Below I try to demonstrate the options and usage of each styling option with at least one example.


In [1]:
import toytree
import toyplot
import numpy as np

# a tree to use for examples
url = "https://eaton-lab.org/data/Cyathophora.tre"
rtre = toytree.tree(url).root(wildcard='prz')

Tip label styling

tip_labels

Type: boolean or list
Allowed: True, False, or list (length of ntips)
Default: True
Description: Shows or hides tip labels (boolean), or replaces tip labels with a user defined list.


In [2]:
# hide tip labels
rtre.draw(tip_labels=False);



In [3]:
# get tip labels from tree
tipnames = rtre.get_tip_labels()

# modify list so that html italic tags surround text
italicnames = ["<i>{}</i>".format(i) for i in tipnames]

# enter the list of names to tip_labels
rtre.draw(tip_labels=italicnames);


<i>38362_rex</i><i>39618_rex</i><i>35236_rex</i><i>35855_rex</i><i>40578_rex</i><i>30556_thamno</i><i>33413_thamno</i><i>41478_cyathophylloides</i><i>41954_cyathophylloides</i><i>30686_cyathophylla</i><i>29154_superba</i><i>33588_przewalskii</i><i>32082_przewalskii</i>

tip_labels_align

Type: boolean
Allowed: True, False
Default: False
Description: Add lines connecting tips to tip labels. See edge_align_style for ways to style the extra tip lines (colors, etc.).


In [4]:
rtre.draw(tip_labels_align=True);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

tip_labels_colors

Type: (str, tuple, list)
Allowed: Any color values supported in toyplot colors.
Default: "#262626" (near black)
Description: A single value can be applied to color all tip labels, or a list of values can be entered to apply different colors to different tip labels. The order of colors is applied to tip labels in the same order as .get_tip_labels() returns the labels. This order (the plot order) is from the tip located on the zero-axis (e.g., x=0 for right-facing trees) and continuing until the last name. If both tip_labels_colors and tip_labels_style["fill"] are used tip_labels_colors overrides the other. In contrast to the fill style, only this option can be used to apply multiple colors.


In [5]:
# use color from favored toytree color scheme
rtre.draw(
    tip_labels_align=True, 
    tip_labels_colors=toytree.colors[1],
);

# enter a list of colors by name
rtre.draw(
    tip_labels_align=True, 
    tip_labels_colors=(['goldenrod'] * 11) + (["mediumseagreen"] * 2),
);

# make list of hex color values based on tip labels
colorlist = ["#d6557c" if "rex" in tip else "#5384a3" for tip in rtre.get_tip_labels()]
rtre.draw(
    tip_labels_align=True, 
    tip_labels_colors=colorlist
);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

tip_labels_style

Type: dictionary of (mostly) CSS styles.
Allowed: CSS styles accepted by toyplot. For example, you could add "stroke": "red" to draw a stroke around the text. The defaults provide just the most likely styles.
Default: Defaults are shown below.
Description: 'fill' is the color of the text; 'font-size' should be entered in units of pixels; '-toyplot-anchor-shift' can be used to shift text closer or farther from the tree tips.


In [6]:
rtre.draw(
    tip_labels_style={
        "fill": "#262626", 
        "font-size": "11px", 
        "-toyplot-anchor-shift": "15px", 
    }
);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

Node labels styling

node_labels

Type: boolean, str, or list of strings or ints.
Allowed: True, False, string name of features (e.g., "idx", "support"), or list that is the same length as the number of nodes. Default: False
Description: If True then the node index labels are used.


In [7]:
# shows node idx labels on all nodes
rtre.draw(node_labels=True);


012345678910111213141516171819202122232438362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

In [8]:
# suppreses nodes
rtre.draw(node_labels=False);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

In [9]:
# suppresses node labels, sizes ensures nodes are still shown
rtre.draw(node_labels=False, node_sizes=10);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

In [10]:
# shortcut for 'default' features always present in TreeNodes, suppresses tip nodes.
rtre.draw(node_labels="support");


100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

In [11]:
# build a list of values in the correct node plot order
sups = rtre.get_node_values("support", show_root=True, show_tips=True)
rtre.draw(node_labels=sups);


0000000000000100961009910010010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

node_labels_style

Type: dictionary.
Allowed: CSS values supported for text by toyplot.
Default: fill: near-black, font-size: '11px'.
Description: A dictionary of CSS style options applied to text of node labels.


In [12]:
rtre.draw(
    node_labels='idx',
    node_labels_style={
        "fill": "#262626",
        "font-size": "8px",
    }
);


131415161718192021222338362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

Node styling

node_sizes


In [13]:
rtre.draw(
    node_labels=False,
    node_sizes=10,
);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

In [14]:
# draw random values to use for node sizes
np.random.seed(1234)
sizes = np.random.uniform(5, 15, rtre.nnodes)
rtre.draw(
    node_labels=False,
    node_sizes=sizes,
);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

node_colors


In [15]:
# set a single color for all nodes
rtre.draw(
    node_labels=False,
    node_sizes=10, 
    node_colors=toytree.colors[1],
);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

In [20]:
rtre.get_node_values("support", 1, 0)


Out[20]:
array(['100', '100', '100', '100', '100', '100', '100', '100', '99',
       '100', '96', '100', '', '', '', '', '', '', '', '', '', '', '', '',
       ''], dtype='<U21')

In [22]:
# get list of sizes and colors in node plot order with tip nodes suppressed
sizes = [10 if i else 0 for i in rtre.get_node_values('support', 1, 0)]
colors = ['black' if i=='100' else 'red' for i in rtre.get_node_values('support', 1, 0)]

# enter a lists of values
rtre.draw(
    node_labels=None,
    node_sizes=sizes, 
    node_colors=colors,
);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

node_markers

See toyplot markers for available options.


In [23]:
# enter a lists of values
rtre.draw(
    node_labels="support",
    node_markers="o"
);


100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

In [24]:
# enter a lists of values
rtre.draw(
    height=350,
    node_labels=None,
    node_sizes=[0 if i else 8 for i in rtre.get_node_values(None, 1, 0)],
    node_markers="s",
    node_colors=toytree.colors[1],
);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

Rectangular markers can be drawn in many dimensions. Designate "r2x1" for a box that is twice as wide as it is tall.


In [25]:
# rectangles for nodes
rtre.draw(
    width=600,
    height=400,
    node_labels="support",
    node_labels_style={"font-size": "11px"},
    node_markers="r2x1.25",
    node_sizes=12,
);


100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

node_style


In [26]:
# the classic "ape" style tree plot look
rtre.draw(
    width=600,
    height=400,
    node_labels="support",
    node_labels_style={"font-size": "10px"},
    node_markers="r2x1.25",
    node_sizes=12,
    node_style={
        "fill": "lightgrey", 
        "stroke": "black",
        "stroke-width": 0.75,
    }
);


100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

node_hover

Enables interactive hover over nodes so that you can see all features associated with each.


In [27]:
rtre.draw(node_hover=True, node_labels=True, node_sizes=15);


idx: 0 dist: 0.0011 support: 0.0000 height: 0.0000 name: 38362_rex0idx: 1 dist: 0.0010 support: 0.0000 height: 0.0001 name: 39618_rex1idx: 2 dist: 0.0058 support: 0.0000 height: 0.0015 name: 35236_rex2idx: 3 dist: 0.0034 support: 0.0000 height: 0.0024 name: 35855_rex3idx: 4 dist: 0.0034 support: 0.0000 height: 0.0024 name: 40578_rex4idx: 5 dist: 0.0065 support: 0.0000 height: 0.0023 name: 30556_thamno5idx: 6 dist: 0.0057 support: 0.0000 height: 0.0042 name: 33413_thamno6idx: 7 dist: 0.0001 support: 0.0000 height: 0.0028 name: 41478_cyathophylloides7idx: 8 dist: 0.0001 support: 0.0000 height: 0.0027 name: 41954_cyathophylloides8idx: 9 dist: 0.0067 support: 0.0000 height: 0.0032 name: 30686_cyathophylla9idx: 10 dist: 0.0063 support: 0.0000 height: 0.0035 name: 29154_superba10idx: 11 dist: 0.0025 support: 0.0000 height: 0.0127 name: 33588_przewalskii11idx: 12 dist: 0.0026 support: 0.0000 height: 0.0126 name: 32082_przewalskii12idx: 13 dist: 0.0062 support: 100.0000 height: 0.0011 name: 1313idx: 14 dist: 0.0007 support: 96.0000 height: 0.0073 name: 1414idx: 15 dist: 0.0022 support: 100.0000 height: 0.0058 name: 1515idx: 16 dist: 0.0008 support: 99.0000 height: 0.0080 name: 1616idx: 17 dist: 0.0010 support: 100.0000 height: 0.0088 name: 1717idx: 18 dist: 0.0094 support: 100.0000 height: 0.0028 name: 2318idx: 19 dist: 0.0024 support: 100.0000 height: 0.0099 name: 2119idx: 20 dist: 0.0054 support: 100.0000 height: 0.0098 name: 1820idx: 21 dist: 0.0030 support: 100.0000 height: 0.0122 name: 2221idx: 22 dist: 0.0179 support: 100.0000 height: 0.0152 name: 2022idx: 23 dist: 0.0179 support: 100.0000 height: 0.0152 name: 1923idx: 24 dist: 1.0000 support: 100.0000 height: 0.0331 name: root2438362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

Layout

layout

Type: str
Allowed: 'circular', 'c', 'right', 'r', 'down', 'd', (coming: 'unrooted', 'up', 'left')
Default: 'r'
Description: The layout orients the direction of tip labels. Options include linear trees facing right, left, up, down, as well as circular and unrooted trees.


In [28]:
ttre = toytree.rtree.coaltree(20, seed=123)
ttre.draw(
    layout='d', 
    tip_labels_align=True,
    node_sizes=[8 if i else 0 for i in ttre.get_node_values()],
    node_style={"stroke": "black"},
    width=350,
    height=300,
);


r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19

In [37]:
ttre = toytree.rtree.unittree(40, seed=123)
ttre.draw(
    layout='c', 
    edge_type='c',
    node_sizes=[8 if i else 0 for i in ttre.get_node_values()],
    node_style={"stroke": "black"},
    width=400,
    height=400,
);


r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19r20r21r22r23r24r25r26r27r28r29r30r31r32r33r34r35r36r37r38r39

In [38]:
ttre = toytree.rtree.unittree(40, seed=123)
ttre.draw(
    layout='c', 
    edge_type='p',
    width=400,
    height=400,
);


r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19r20r21r22r23r24r25r26r27r28r29r30r31r32r33r34r35r36r37r38r39

todo

Type:
Allowed:
Default:
Description:


In [ ]:

Aligned Edge Styling

edge_align_style

Type:
Allowed:
Default:
Description:


In [39]:
rtre.draw(
    tip_labels_align=True, 
    edge_align_style={
        "stroke": "violet",
        "stroke-width": 1.5,
        "stroke-dasharray": "2,5"    # size of dash, spacing of dashes
    });


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

Styles


In [40]:
rtre.draw(tree_style='n');


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii

In [41]:
# generate a random coalescent tree and draw in 'coalescent' style
randomtre = toytree.rtree.coaltree(ntips=10, seed=666)
randomtre.draw(tree_style='c');


0.00.20.4

Scalebar

You can add a scalebar to any tree plot by simply adding scalebar=True. Alternatively, you can add or customize axes by saving the returned variables from the .draw() function and modifying the axes.


In [42]:
rtre.draw(scalebar=True);


38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii0.000.020.03