draw options
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.
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(13) #13th node corresponds to przewalskii outgroup
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.
# hide tip labels
rtre.draw(tip_labels=False);
# 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);
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.).
rtre.draw(tip_labels_align=True);
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.
# use color from favored toytree color scheme
rtre.draw(
tip_labels_align=True,
tip_labels_colors="orange",
);
# 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
);
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.
rtre.draw(
tip_labels_style={
"fill": "#262626",
"font-size": "11px",
"-toyplot-anchor-shift": "15px",
}
);
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.
# shows node idx labels on all nodes
rtre.draw(node_labels=True);
# suppreses nodes
rtre.draw(node_labels=False);
# suppresses node labels, sizes ensures nodes are still shown
rtre.draw(node_labels=False, node_sizes=10);
# shortcut for 'default' features always present in TreeNodes, suppresses tip nodes.
rtre.draw(node_labels="support");
# 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);
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.
rtre.draw(
node_labels='idx',
node_labels_style={
"fill": "#262626",
"font-size": "8px",
}
);
Node styling¶
node_sizes¶
rtre.draw(
node_labels=False,
node_sizes=10,
);
# 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,
);
node_colors¶
# set a single color for all nodes
rtre.draw(
node_labels=False,
node_sizes=10,
node_colors=toytree.colors[1],
);
rtre.get_node_values("support", 1, 0)
array(['100', '100', '100', '100', '100', '100', '100', '100', '99', '100', '96', '100', '', '', '', '', '', '', '', '', '', '', '', '', ''], dtype='<U21')
# 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,
);
node_markers¶
See toyplot markers for available options.
# enter a lists of values
rtre.draw(
node_labels="support",
node_markers="o"
);
# 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],
);
Rectangular markers can be drawn in many dimensions. Designate "r2x1" for a box that is twice as wide as it is tall.
# 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,
);
node_style¶
# 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,
}
);
node_hover¶
Enables interactive hover over nodes so that you can see all features associated with each.
rtre.draw(node_hover=True, node_labels=True, node_sizes=15);
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.
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,
);
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,
);
ttre = toytree.rtree.unittree(40, seed=123)
ttre.draw(
layout='c',
edge_type='p',
width=400,
height=400,
);
todo
¶
Type:
Allowed:
Default:
Description:
Aligned Edge Styling¶
edge_align_style¶
Type:
Allowed:
Default:
Description:
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
});
Styles¶
rtre.draw(tree_style='n');
# generate a random coalescent tree and draw in 'coalescent' style
randomtre = toytree.rtree.coaltree(ntips=10, seed=666)
randomtre.draw(tree_style='c');
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.
rtre.draw(scalebar=True);