coordinates and axes
Drawing: The Canvas, Axes, and coordinates¶
When you call the toytree.draw()
function it returns two Toyplot objects which are used to display the figure. The first is the Canvas, which is the HTML element that holds the figure, and the second is a Cartesian axes object, which represent the coordinates for the plot. You can store these objects when they are returned by the draw()
function to further manipulate the plot. Storing the Canvas is necessary in order to save the plot.
The Canvas and Axes¶
If you wish to combine multiple toytree figures into a single figure then it is easiest to first create instances of the toyplot Canvas and Axes objects and then to add the toytree drawing to this plot by using the .draw(axes=axes)
argument. In the example below we first define the Canvas size, then define two coordinate axes inside of this Canvas, and then we pass these coordinate axes objects to two separate toytree drawings.
import toytree
import toyplot
import numpy as np
tre = toytree.tree("https://eaton-lab.org/data/Cyathophora.tre")
rtre = tre.root(13) #13th node corresponds to przewalskii outgroup
# set dimensions of the canvas
canvas = toyplot.Canvas(width=700, height=250)
# dissect canvas into multiple cartesian areas (x1, x2, y1, y2)
ax0 = canvas.cartesian(bounds=('10%', '45%', '10%', '90%'))
ax1 = canvas.cartesian(bounds=('55%', '90%', '10%', '90%'))
# call draw with the 'axes' argument to pass it to a specific cartesian area
style = {
"tip_labels_align": True,
"tip_labels_style": {
"font-size": "9px"
},
}
rtre.draw(axes=ax0, **style);
rtre.draw(axes=ax1, ts='o', layout='r', tip_labels_colors="indigo");
#optional: choose to hide the axes markers
ax0.show = False
ax1.show = False
The Coordinates¶
Toytrees drawings are designed to use a set coordinate space within the axes to make it easy to situate additional plots to align with tree drawings. Regardless of whether the tree drawing is oriented 'right' or 'down' the farthest tip of the tree (not tip label but tip) will align at the zero-axis. For right-facing trees this means at x=0, for down-facing trees this means y=0. On the other axis, tree tips will be spaced from zero to ntips with a unit of 1 between each tip. Below I add a grid to overlay tree plots in both orientations to highlight the coordinate space.
# store the returned Canvas and Axes objects
canvas, axes, mark = tre.draw(
width=300,
height=300,
edge_style={"stroke-opacity": 0.5},
tip_labels=False,
node_mask=[False] + [True] * (tre.nnodes - 1),
node_sizes=14,
node_labels="idx",
scale_bar = True
)
# overlay a grid
axes.hlines(np.arange(0, tre.ntips), style={"stroke": "red", "stroke-dasharray": "2,4"})
axes.vlines(0, style={"stroke": "blue", "stroke-dasharray": "2,4"});
# store the returned Canvas and Axes objects
canvas, axes, mark = tre.draw(
width=300,
height=300,
tip_labels=False,
edge_style={"stroke-opacity": 0.5},
node_mask=[False] + [True] * (tre.nnodes - 1),
node_sizes=14,
node_labels="idx",
layout='d',
scale_bar = True
)
# overlay a grid on the axes
axes.vlines(np.arange(0, tre.ntips), style={"stroke": "red", "stroke-dasharray": "2,4"})
axes.hlines(0, style={"stroke": "blue", "stroke-dasharray": "2,4"});