ToyTree with barplot
ToyTree + barplot¶
Aligning a tree with data is sometimes easier on one axis versus two. See the (#1) and (#2) for comparison. Here when plotting on one axis the tree coordinates which map to treeheight and the number of tips can be difficult to align with data (e.g., a barplot) since the data values may be much greater than the treeheight. This can be fixed by tranforming the data and the axis labels. The example on a two axes is a bit easier in this case.
In [2]:
Copied!
import numpy as np
import toytree
import toyplot
# generate a random tree and data
ntips = 20
rseed = 123456
rng = np.random.default_rng(rseed)
# get tree and data
rtre = toytree.rtree.unittree(ntips=ntips, seed=rseed).ladderize()
randomdata = rng.uniform(20, 200, ntips)
# set up a toyplot Canvas with 2 axes: (x1, x2, y1, y2)
canvas = toyplot.Canvas(width=375, height=350)
ax0 = canvas.cartesian(bounds=(50, 200, 50, 300), padding=15, ymin=0, ymax=20)
ax1 = canvas.cartesian(bounds=(225, 325, 50, 300), padding=15, ymin=0, ymax=20)
# add tree to first axes
rtre.draw(axes=ax0);
ax0.show = False
# plot the barplot on the second axes
# (y-axis is range 0-ntips);
# (x-axis is bar values transformed to be 0-1)
# baseline is the space between tipnames and bars
ax1.bars(
np.arange(ntips),
randomdata,
along='y',
);
# style axes
ax1.show = True
ax1.y.show = False
ax1.x.ticks.show = True
import numpy as np
import toytree
import toyplot
# generate a random tree and data
ntips = 20
rseed = 123456
rng = np.random.default_rng(rseed)
# get tree and data
rtre = toytree.rtree.unittree(ntips=ntips, seed=rseed).ladderize()
randomdata = rng.uniform(20, 200, ntips)
# set up a toyplot Canvas with 2 axes: (x1, x2, y1, y2)
canvas = toyplot.Canvas(width=375, height=350)
ax0 = canvas.cartesian(bounds=(50, 200, 50, 300), padding=15, ymin=0, ymax=20)
ax1 = canvas.cartesian(bounds=(225, 325, 50, 300), padding=15, ymin=0, ymax=20)
# add tree to first axes
rtre.draw(axes=ax0);
ax0.show = False
# plot the barplot on the second axes
# (y-axis is range 0-ntips);
# (x-axis is bar values transformed to be 0-1)
# baseline is the space between tipnames and bars
ax1.bars(
np.arange(ntips),
randomdata,
along='y',
);
# style axes
ax1.show = True
ax1.y.show = False
ax1.x.ticks.show = True