Tree annotations¶
The .annotate
subpackage offers a clean, readable way to edit an existing tree as an alternative to modifying each argument in the .draw()
method. The funcitons provided in this subpackage work by adding marks on top of the most recent canvas
created by Toytree. It can be accessed direclty from a tree object (e.g., tree.annotate.{function}()), but some functions require additional arguments to specify axes, styles, etc.
Since this subpackage contains very simple modifications that can be quickly added on top of existing trees, we encourage you to share with us any functions you make that may fit in this subpackage! These can be shared via Github at https://github.com/eaton-lab/toytree/discussions
Quick examples¶
In [1]:
Copied!
import toytree
import numpy as np
import toytree
import numpy as np
In [2]:
Copied!
#add_axes_box_outline
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
tree.annotate.add_axes_box_outline(axes=axes)
#add_axes_box_outline
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
tree.annotate.add_axes_box_outline(axes=axes)
Out[2]:
<toyplot.coordinates.Cartesian at 0x210a26ad6d0>
In [3]:
Copied!
#add_edge_labels
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
m1 = tree.annotate.add_edge_labels(
axes,
labels=tree.get_node_data("idx"),
color='blue',
style={'font-size': 16, 'baseline-shift': 8}
)
#add_edge_labels
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
m1 = tree.annotate.add_edge_labels(
axes,
labels=tree.get_node_data("idx"),
color='blue',
style={'font-size': 16, 'baseline-shift': 8}
)
In [4]:
Copied!
#add_edge_markers
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
m1 = tree.annotate.add_edge_markers(
axes,
marker='s',
size=9,
color='red',
style={'stroke': 'white', 'stroke-width': 2.5}
)
#add_edge_markers
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
m1 = tree.annotate.add_edge_markers(
axes,
marker='s',
size=9,
color='red',
style={'stroke': 'white', 'stroke-width': 2.5}
)
In [5]:
Copied!
#add_node_markers
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
# add markers to all Nodes
m1 = tree.annotate.add_node_markers(
axes,
marker='s',
size=9,
color='red',
style={'stroke': 'white', 'stroke-width': 2.5}
)
# add markers to only a few Nodes
m2 = tree.annotate.add_node_markers(
axes, marker=">", size=20, mask=tree.get_node_mask(9, 10)
)
#add_node_markers
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
# add markers to all Nodes
m1 = tree.annotate.add_node_markers(
axes,
marker='s',
size=9,
color='red',
style={'stroke': 'white', 'stroke-width': 2.5}
)
# add markers to only a few Nodes
m2 = tree.annotate.add_node_markers(
axes, marker=">", size=20, mask=tree.get_node_mask(9, 10)
)
In [6]:
Copied!
#add_node_labels
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
m1 = tree.annotate.add_node_labels(
axes,
labels=tree.get_node_data("idx"),
color='blue',
style={'font-size': 16, 'baseline-shift': 8}
)
#add_node_labels
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
m1 = tree.annotate.add_node_labels(
axes,
labels=tree.get_node_data("idx"),
color='blue',
style={'font-size': 16, 'baseline-shift': 8}
)
In [7]:
Copied!
#add_node_bars
tree = toytree.rtree.unittree(10, treeheight=1e5)
c, a, m = tree.draw()
node_height = tree.get_node_data("height").values
tree.annotate.add_node_bars(
axes=a,
bar_min=node_height * 0.5,
bar_max=node_height * 1.5,
size=0.33, z_index=-1, color='purple', opacity=0.4,
)
#add_node_bars
tree = toytree.rtree.unittree(10, treeheight=1e5)
c, a, m = tree.draw()
node_height = tree.get_node_data("height").values
tree.annotate.add_node_bars(
axes=a,
bar_min=node_height * 0.5,
bar_max=node_height * 1.5,
size=0.33, z_index=-1, color='purple', opacity=0.4,
)
Out[7]:
<toytree.drawing.src.mark_annotation.AnnotationRect at 0x210a540f2f0>
In [8]:
Copied!
#add_tip_markers
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
# add markers to all Nodes
m1 = tree.annotate.add_tip_markers(
axes,
marker='s',
size=9,
color='red',
style={'stroke': 'white', 'stroke-width': 2.5}
)
# add markers to only a few Nodes
m2 = tree.annotate.add_tip_markers(
axes, marker=">", size=20, mask=tree.get_node_mask(9)
)
#add_tip_markers
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
# add markers to all Nodes
m1 = tree.annotate.add_tip_markers(
axes,
marker='s',
size=9,
color='red',
style={'stroke': 'white', 'stroke-width': 2.5}
)
# add markers to only a few Nodes
m2 = tree.annotate.add_tip_markers(
axes, marker=">", size=20, mask=tree.get_node_mask(9)
)
In [12]:
Copied!
#add_axes_scale_bar
import toytree
tree = toytree.rtree.unittree(ntips = 10)
canvas, axes, mark = tree.draw()
tree.annotate.add_axes_scale_bar(axes)
#add_axes_scale_bar
import toytree
tree = toytree.rtree.unittree(ntips = 10)
canvas, axes, mark = tree.draw()
tree.annotate.add_axes_scale_bar(axes)
Out[12]:
<toyplot.coordinates.Cartesian at 0x210a3067d10>
In [10]:
Copied!
#add_node_pie_charts
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
# generate random pie-like (proportion) data array
ncategories = 3
arr = np.random.random(size=(tree.nnodes, ncategories))
arr = (arr.T / arr.sum(axis=1)).T
# add pie charts to all internal Nodes
tree.annotate.add_node_pie_charts(
axes=axes, data=arr, size=20, mask=(0, 1, 1),
istroke_width=0.75, istroke="black", rotate=-45,
)
#add_node_pie_charts
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
# generate random pie-like (proportion) data array
ncategories = 3
arr = np.random.random(size=(tree.nnodes, ncategories))
arr = (arr.T / arr.sum(axis=1)).T
# add pie charts to all internal Nodes
tree.annotate.add_node_pie_charts(
axes=axes, data=arr, size=20, mask=(0, 1, 1),
istroke_width=0.75, istroke="black", rotate=-45,
)
Out[10]:
<toytree.drawing.src.mark_pie.PieChartMark at 0x210a542ad20>
In [11]:
Copied!
#add_edge_pie_charts
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
# generate random pie-like (proportion) data array
ncategories = 3
arr = np.random.random(size=(tree.nnodes, ncategories))
arr = (arr.T / arr.sum(axis=1)).T
# add pie charts to all internal Nodes
tree.annotate.add_edge_pie_charts(
axes=axes, data=arr, size=20, mask=(0, 1, 1),
istroke_width=0.75, istroke="black", rotate=-45,
)
#add_edge_pie_charts
tree = toytree.rtree.unittree(6, seed=123)
canvas, axes, m0 = tree.draw()
# generate random pie-like (proportion) data array
ncategories = 3
arr = np.random.random(size=(tree.nnodes, ncategories))
arr = (arr.T / arr.sum(axis=1)).T
# add pie charts to all internal Nodes
tree.annotate.add_edge_pie_charts(
axes=axes, data=arr, size=20, mask=(0, 1, 1),
istroke_width=0.75, istroke="black", rotate=-45,
)
Out[11]:
<toytree.drawing.src.mark_pie.PieChartMark at 0x210a4e736b0>