-
Notifications
You must be signed in to change notification settings - Fork 29
Method `baltic.tree.addText`
Barney Potter edited this page Oct 14, 2024
·
1 revision
The addText()
method in the BALTIC tree
class is used to add text annotations to a tree plot. This method allows for flexible positioning and customization of text elements, making it useful for labeling tips, adding node information, or annotating specific parts of the tree.
def addText(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 theleaf
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. - Generates the text content using the
text
function. - Adds the text to the plot using matplotlib's
ax.text
method.
- Determines the x and y coordinates using
- Returns the modified axes object.
- Labeling tips with species names or sample IDs.
- Adding support values or other statistics to internal nodes.
- Annotating specific branches or clades with additional information.
- Creating custom legends or explanatory text on the tree plot.
import matplotlib.pyplot as plt
# Create a figure and axes
fig, ax = plt.subplots(figsize=(10, 8))
# Plot the tree
tree.plotTree(ax)
# Add tip labels
tree.addText(ax, target=lambda n: n.is_leaf(), x_attr=lambda n: n.x + 0.1, ha='left')
# Add posterior probabilities to internal nodes
tree.addText(ax,
target=lambda n: n.is_node() and 'posterior' in n.traits,
x_attr=lambda n: n.x - 0.1,
y_attr=lambda n: n.y + 0.1,
text=lambda n: f"{n.traits['posterior']:.2f}",
ha='right',
va='bottom',
fontsize=8)
# Add a custom annotation to a specific node
special_node = tree.getInternal()[5] # Example: 6th internal node
tree.addText(ax,
target=lambda n: n == special_node,
text=lambda n: "Important\nClade",
bbox=dict(facecolor='white', edgecolor='red', boxstyle='round,pad=0.5'))
plt.show()
- The
target
,x_attr
,y_attr
, andtext
parameters accept functions, allowing for great flexibility in determining what to annotate and how. - If not specified, the default behavior is to add the names of all leaf nodes at their respective positions.
- 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 large trees, adding text to every node might clutter the visualization. Consider using
target
to selectively annotate important features. - The method assumes that the tree has been previously drawn (e.g., using
plotTree()
) to ensure that x and y coordinates are available. - This 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