-
Notifications
You must be signed in to change notification settings - Fork 29
Method `baltic.tree.addTextUnrooted`
Barney Potter edited this page Oct 14, 2024
·
1 revision
The addTextUnrooted()
method in the BALTIC tree
class is used to add text annotations to an unrooted tree plot. This method is specifically designed to handle the circular layout of unrooted trees, adjusting text orientation and position accordingly.
def addTextUnrooted(self, ax, target=None, x_attr=None, y_attr=None, text=None, zorder=None, **kwargs)
-
ax
(matplotlib.axes.Axes): The matplotlib axes to add the text to. -
target
(function or None): A function to select which branches to annotate. Default is None, which selects allleaf
nodes. -
x_attr
(function or None): A function to determine the x-coordinate for the text. Default is None, which uses the branch's x attribute. -
y_attr
(function or None): A function to determine the y-coordinate for the text. Default is None, which uses the branch's y attribute. -
text
(function or None): A function to determine the text content. Default is None, which uses the branch's name attribute. -
zorder
(int or None): The z-order for the text. Default is None, which sets the z-order to 4. -
**kwargs
: Additional keyword arguments to pass to theax.text
method.
- matplotlib.axes.Axes: The axes with the text annotations added.
- Iterates through branches that satisfy the
target
condition. - For each selected branch:
- Determines the x and y coordinates using
x_attr
andy_attr
functions. - Calculates the appropriate rotation angle based on the branch's position in the circular layout.
- Adjusts text alignment for readability based on its position.
- Generates the text content using the
text
function. - Adds the rotated and aligned text to the plot using matplotlib's
ax.text
method.
- Determines the x and y coordinates using
- Returns the modified axes object.
- Labeling tips in an unrooted tree visualization with species names or sample IDs.
- Adding annotations to specific branches or clades in a radial tree layout.
- Creating circular phylogenetic visualizations with readable labels.
- Enhancing unrooted tree plots with additional information like support values or traits.
import matplotlib.pyplot as plt
# Create a figure and axes
fig, ax = plt.subplots(figsize=(12, 12))
# Draw the unrooted tree
tree.drawUnrooted()
tree.plotTree(ax)
# Add tip labels
tree.addTextUnrooted(ax,
target=lambda n: n.is_leaf(),
x_attr=lambda n: n.x * 1.1, # Slightly outside the tips
y_attr=lambda n: n.y * 1.1,
fontsize=8)
# Add support values to internal nodes
tree.addTextUnrooted(ax,
target=lambda n: n.is_node() and 'posterior' in n.traits,
x_attr=lambda n: n.x * 0.9, # Slightly inside the nodes
y_attr=lambda n: n.y * 0.9,
text=lambda n: f"{n.traits['posterior']:.2f}",
fontsize=6,
color='red')
plt.axis('equal') # Ensure the plot is circular
plt.axis('off') # Hide axes
plt.tight_layout()
plt.show()
- This method is specifically designed for use with unrooted (circular) tree layouts. Make sure to use
drawUnrooted()
before calling this method. - The method automatically handles text rotation and alignment to ensure readability in the circular layout.
- The
target
,x_attr
,y_attr
, andtext
parameters accept functions, allowing for great flexibility in determining what to annotate and how. - For best results, adjust the x and y attributes to position text slightly inside or outside the tree circle as needed.
- The
zorder
parameter can be used to control whether the text appears above or below other elements in the plot. - Additional text formatting can be controlled through the
**kwargs
, which are passed directly to matplotlib'stext()
function. - For dense trees, consider using the
target
parameter to selectively annotate to avoid overcrowding. - This method assumes that the tree has been drawn in an unrooted layout and that each node has a
tau
attribute representing its angular position. - The method does not modify the tree structure; it only adds visual elements to the plot.
Wiki written with the assistance of claude.ai 3.5 "Sonnet".
- Core
baltic
classes:-
Class
tree
- Tree Construction and Manipulation methods
- Tree Analysis methods
- Tree Conversion and Output methods
- Tree Visualization
- Utility Methods
- Class
leaf
- Class
node
- Class
clade
- Class
reticulation
-
Class