Visualization¶
plot_tree(tree: Tree, *, backend: Literal['matplotlib', 'plotly'] = 'matplotlib', path: str | os.PathLike | None = None, show: bool = False, edge_length_mode: EdgeLengthMode | Literal['attr', 'uniform', 'even', 'rank'] = EdgeLengthMode.ATTR, edge_length_attr: str = 'dist', layout_mode: LayoutMode | Literal['horizontal', 'vertical', 'circular'] = LayoutMode.HORIZONTAL, node_rank_mode: NodeRankMode | Literal['mean', 'first', 'last', 'node'] = NodeRankMode.MEAN, tree_style: TreeStyle | None = None, **renderer_kwargs: Any) -> tuple[Figure, Axes] | PlotlyFigure
¶
Lay out and render tree in a single call.
Convenience wrapper around :class:~tralda.visualization.layout.TreeLayout and
:class:~tralda.visualization.matplotlib_renderer.MatplotlibRenderer /
:class:~tralda.visualization.plotly_renderer.PlotlyRenderer. Construct those objects directly
for more control (e.g. drawing onto an existing matplotlib Axes, or reusing one layout with
several styles).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
The tree to render. |
required |
backend
|
Literal['matplotlib', 'plotly']
|
|
'matplotlib'
|
path
|
str | PathLike | None
|
If given, save the resulting figure to this path. For the matplotlib backend the
format is inferred from the file extension by
:meth: |
None
|
show
|
bool
|
If |
False
|
edge_length_mode
|
EdgeLengthMode | Literal['attr', 'uniform', 'even', 'rank']
|
How edge lengths are determined. See
:class: |
ATTR
|
edge_length_attr
|
str
|
Node attribute read as edge length in |
'dist'
|
layout_mode
|
LayoutMode | Literal['horizontal', 'vertical', 'circular']
|
Tree orientation. See :class: |
HORIZONTAL
|
node_rank_mode
|
NodeRankMode | Literal['mean', 'first', 'last', 'node']
|
How the rank of internal nodes is derived from their children. See
:class: |
MEAN
|
tree_style
|
TreeStyle | None
|
Styling configuration. When |
None
|
**renderer_kwargs
|
Any
|
Additional keyword arguments forwarded to the renderer constructor
(e.g. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[Figure, Axes] | Figure
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If backend is not |
ImportError
|
If the optional dependency required by backend is not installed. |
Source code in tralda/visualization/_convenience.py
Layout¶
TreeLayout
¶
Computes and stores the geometric layout of a tree for visualization.
Depths and leaf ranks are computed once during construction. The final screen-space positions
are derived from these canonical coordinates according to the chosen :class:LayoutMode and
stored in :attr:positions. Calling :meth:compute_positions again with a different
:class:LayoutMode re-derives the positions without repeating the depth computation.
Attributes:
| Name | Type | Description |
|---|---|---|
tree |
Tree
|
The tree being laid out. |
edge_length_mode |
EdgeLengthMode
|
The edge-length mode used during construction. |
edge_length_attr |
str
|
The node attribute read as edge length in |
layout_mode |
LayoutMode
|
The current layout orientation. |
node_rank_mode |
NodeRankMode
|
How the rank of internal nodes is derived. |
depths |
dict[TreeNode, float]
|
Canonical depth of every node from the root. |
leaf_ranks |
dict[TreeNode, float]
|
Perpendicular rank of every node. Leaves receive
integer ranks |
max_depth |
float
|
Maximum depth value (depth at which the outermost leaves sit). |
leaf_count |
int
|
Total number of leaves. |
rank_span |
int
|
Number of distinct perpendicular rank slots occupied by nodes. Equals
leaf_count for all :class: |
positions |
dict[TreeNode, tuple[float, float]]
|
Final |
label_angle |
dict[TreeNode, float]
|
Suggested text rotation in degrees for each node. 0 = horizontal text; positive values rotate counter-clockwise. |
label_ha |
dict[TreeNode, str]
|
Suggested |
label_va |
dict[TreeNode, str]
|
Suggested |
Source code in tralda/visualization/layout.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
__init__(tree: Tree, edge_length_mode: EdgeLengthMode | Literal['attr', 'uniform', 'even', 'rank'] = EdgeLengthMode.ATTR, edge_length_attr: str = 'dist', layout_mode: LayoutMode | Literal['horizontal', 'vertical', 'circular'] = LayoutMode.HORIZONTAL, node_rank_mode: NodeRankMode | Literal['mean', 'first', 'last', 'node'] = NodeRankMode.MEAN) -> None
¶
Construct a layout for the given tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
Tree
|
The tree to lay out. |
required |
edge_length_mode
|
EdgeLengthMode | Literal['attr', 'uniform', 'even', 'rank']
|
How edge lengths along the depth axis are determined. Accepts an
:class: |
ATTR
|
edge_length_attr
|
str
|
Name of the :class: |
'dist'
|
layout_mode
|
LayoutMode | Literal['horizontal', 'vertical', 'circular']
|
Orientation of the resulting drawing. Accepts a :class: |
HORIZONTAL
|
node_rank_mode
|
NodeRankMode | Literal['mean', 'first', 'last', 'node']
|
How the perpendicular rank of internal nodes is derived from their
children. Accepts a :class: |
MEAN
|
Source code in tralda/visualization/layout.py
parent_edge(node: TreeNode) -> tuple[tuple[float, float], tuple[float, float]] | None
¶
Return the screen-space endpoints of the edge from a node to its parent.
For CIRCULAR mode the returned coordinates are straight-line Cartesian endpoints;
renderers are responsible for drawing the elbow (radial segment + arc).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TreeNode
|
The node whose parent edge is requested. |
required |
Returns:
| Type | Description |
|---|---|
tuple[tuple[float, float], tuple[float, float]] | None
|
A tuple |
Source code in tralda/visualization/layout.py
children_connector(node: TreeNode) -> tuple[tuple[float, float], tuple[float, float]] | None
¶
Return the screen-space endpoints of the connector between a node's outermost children.
In HORIZONTAL mode this is a vertical line segment; in VERTICAL mode a horizontal
segment. In CIRCULAR mode the two endpoints on the arc at the parent radius are
returned — renderers should draw a circular arc rather than a straight line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TreeNode
|
The internal node whose child connector is requested. |
required |
Returns:
| Type | Description |
|---|---|
tuple[tuple[float, float], tuple[float, float]] | None
|
A tuple |
tuple[tuple[float, float], tuple[float, float]] | None
|
leaf. |
Source code in tralda/visualization/layout.py
ghost_segment(node: TreeNode) -> tuple[tuple[float, float], tuple[float, float]] | None
¶
Return the screen-space endpoints of the ghost (leaf-extension) segment for a node.
A ghost segment is a dashed or otherwise styled line that extends a short-branched leaf
to the maximum depth so that all leaves are visually aligned. It is only relevant for edge
length modes where leaves can end at different depths (ATTR or UNIFORM). It is the
responsibility of renderers to decide whether to draw a ghost segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TreeNode
|
The node for which the ghost segment is requested. |
required |
Returns:
| Type | Description |
|---|---|
tuple[tuple[float, float], tuple[float, float]] | None
|
A tuple |
Source code in tralda/visualization/layout.py
compute_positions(layout_mode: LayoutMode | Literal['horizontal', 'vertical', 'circular'] | None = None) -> None
¶
Transform canonical coordinates into screen-space positions.
Called automatically during construction. Can be called again to switch layout modes without recomputing depths and leaf ranks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout_mode
|
LayoutMode | Literal['horizontal', 'vertical', 'circular'] | None
|
Override the layout mode set during construction. Accepts a
:class: |
None
|
Source code in tralda/visualization/layout.py
EdgeLengthMode
¶
Bases: Enum
How edge lengths are determined along the depth axis.
Attributes:
| Name | Type | Description |
|---|---|---|
ATTR |
Use a node attribute for the length of the edge to its parent. |
|
UNIFORM |
Every edge has length 1. Leaves may end at different depths. |
|
EVEN |
Edges are scaled so that all leaves end at the same depth (cladogram style). Each internal node is placed by distributing the remaining depth evenly among the remaining edges on the deepest root-to-leaf path through that node. |
|
RANK |
Internal nodes are placed at their topological depth (number of edges from the root). Leaves are extended to the maximum depth so they are all aligned. |
Source code in tralda/visualization/layout.py
LayoutMode
¶
Bases: Enum
Orientation of the tree drawing.
Attributes:
| Name | Type | Description |
|---|---|---|
HORIZONTAL |
Root on the left, leaves on the right. |
|
VERTICAL |
Root at the top, leaves at the bottom. |
|
CIRCULAR |
Root at the centre, leaves on the outside. |
Source code in tralda/visualization/layout.py
NodeRankMode
¶
Bases: Enum
How the perpendicular rank of an internal node is derived from its children's ranks.
Attributes:
| Name | Type | Description |
|---|---|---|
MEAN |
The node is placed at the mean rank of its first and last child (default). |
|
FIRST |
The node is placed at the rank of its first (leftmost) child. |
|
LAST |
The node is placed at the rank of its last (rightmost) child. |
|
NODE |
Every node (leaf or internal) receives its own unique rank in preorder. This guarantees that no two nodes share the same perpendicular position, which eliminates label overlap when labels are drawn for all nodes. |
Source code in tralda/visualization/layout.py
Styling¶
NodeStyle
dataclass
¶
Partial style specification for a single tree node.
Every field defaults to None, which means "inherit from the layer below". Pass a
:class:NodeStyle from a style function or node-override dict to override only the fields you
care about.
Symbol fields
symbol
Name of the symbol to draw (must be registered in :data:~._symbols.SYMBOL_REGISTRY).
symbol_size
Diameter of the symbol in points.
symbol_color
Fill color of the symbol (passed as color to the drawer).
symbol_edge_color
Border color of the symbol (passed as edge_color to the drawer).
symbol_lw
Border line width of the symbol in points.
symbol_zorder
Z-order of the symbol layer.
Incoming-edge fields (edge from parent to this node)
edge_color
Color of the edge from this node's parent.
edge_lw
Line width of the incoming edge in points.
edge_ls
Line style of the incoming edge (e.g. "-", "--", ":", "-.").
Label fields
label_color
Color of the leaf label text.
label_fontsize
Font size in points, or a named size string (e.g. "small").
label_fontweight
Font weight: "normal" or "bold".
label_fontstyle
Font style: "normal" or "italic".
Source code in tralda/visualization/style.py
overlay(other: NodeStyle) -> NodeStyle
¶
Return a new :class:NodeStyle with other's non-None fields applied over self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
NodeStyle
|
The higher-priority partial style. |
required |
Returns:
| Type | Description |
|---|---|
NodeStyle
|
A new :class: |
Source code in tralda/visualization/style.py
TreeStyle
dataclass
¶
Resolved styling configuration for an entire tree.
Attributes:
| Name | Type | Description |
|---|---|---|
default |
NodeStyle
|
Default :class: |
node_symbol |
str
|
Symbol name used for all nodes when the style function and node-override dict
do not specify one. Default |
root_symbol |
str | None
|
Symbol name used for the root node when the style function and node-override
dict do not specify one. Default |
leaf_symbol |
str | None
|
Symbol name used for leaf nodes when not otherwise specified.
Default |
internal_symbol |
str | None
|
Symbol name used for internal (non-root) nodes when not otherwise
specified. Default |
ghost_color |
str
|
Color of ghost (leaf-extension) segments. Default |
ghost_lw |
float
|
Line width of ghost segments. Default |
ghost_ls |
str
|
Line style of ghost segments. Default |
style_fn |
StyleFn | None
|
Optional callable |
node_overrides |
dict[TreeNode, NodeStyle]
|
Optional per-node overrides applied after style_fn (highest priority). |
Source code in tralda/visualization/style.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
resolve(node: TreeNode, layout_mode: LayoutMode) -> NodeStyle
¶
Return the fully-resolved :class:NodeStyle for node.
Applies the default, then the style function, then any direct node override. The
symbol field is filled from the structural fallbacks when it is still None after all
layers have been merged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TreeNode
|
The node to resolve styling for. |
required |
layout_mode
|
LayoutMode
|
Current layout mode, passed to :attr: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
NodeStyle
|
class: |
Source code in tralda/visualization/style.py
consensus_style(nodes: Iterable[TreeNode], layout_mode: LayoutMode) -> NodeStyle
¶
Return a consensus style for a set of nodes (with fallback to default).
If all nodes agree on a particular field (e.g. all have the same symbol or symbol color), the consensus style will use that value. If there's any disagreement, the consensus style falls back to the default for that field. This is useful for styling internal nodes or edges based on the styles of their children (e.g. for styling the child connector line / arc based on the styles of the child nodes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
Iterable[TreeNode]
|
The nodes to find a consensus style for. |
required |
layout_mode
|
LayoutMode
|
Current layout mode, passed to :attr: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
NodeStyle
|
class: |
Source code in tralda/visualization/style.py
from_maps(*, style_attr: str = 'style', style_map: dict[str, NodeStyle] | None = None, symbol_attr: str = 'symbol', symbol_map: dict[str, str] | None = None, node_color_attr: str = 'color', node_color_map: dict[Any, Any] | None = None, edge_color_attr: str = 'edge_color', edge_color_map: dict[Any, Any] | None = None, **kwargs: Any) -> TreeStyle
classmethod
¶
Construct a :class:TreeStyle from attribute lookup maps.
This is a convenience constructor for the common case where styling is determined by
looking up node attributes in provided maps. It translates the maps into a style function
that applies the specified lookups and falls back to the default styling when no match is
found. The kwargs are forwarded to the :class:TreeStyle constructor, so you can still
set defaults and other options.
If provided, style_map is applied first (lowest priority). The other maps may override symbols and colors specified in style_map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style_attr
|
str
|
Node attribute whose value is looked up in style_map to get a full or
partial :class: |
'style'
|
style_map
|
dict[str, NodeStyle] | None
|
Mapping from style_attr values to :class: |
None
|
symbol_attr
|
str
|
Node attribute whose value is looked up in symbol_map.
Default |
'symbol'
|
symbol_map
|
dict[str, str] | None
|
Mapping from symbol_attr values to symbol names. Nodes not in the map use the structural fallbacks. |
None
|
node_color_attr
|
str
|
Node attribute whose value is looked up in node_color_map.
Default |
'color'
|
node_color_map
|
dict[Any, Any] | None
|
Mapping from node_color_attr values to fill colors. |
None
|
edge_color_attr
|
str
|
Node attribute whose value is looked up in edge_color_map.
Default |
'edge_color'
|
edge_color_map
|
dict[Any, Any] | None
|
Mapping from edge_color_attr values to edge colors. |
None
|
**kwargs
|
Any
|
Forwarded to the :class: |
{}
|
Returns:
| Type | Description |
|---|---|
TreeStyle
|
A new :class: |
Source code in tralda/visualization/style.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
DEFAULT_NODE_STYLE = NodeStyle(symbol=None, symbol_size=9.0, symbol_color='white', symbol_edge_color='black', symbol_lw=1.0, symbol_zorder=3, edge_color='black', edge_lw=1.0, edge_ls='-', label_color='black', label_fontsize=9, label_fontweight='normal', label_fontstyle='normal')
module-attribute
¶
Matplotlib backend¶
MatplotlibRenderer
¶
Bases: BaseRenderer
Render a :class:~tralda.visualization.layout.TreeLayout with matplotlib.
All node symbols are drawn at a size specified in points, so they remain visually consistent
regardless of the data-coordinate scale. Depth coordinates are optionally normalised to
[0, 1] before rendering.
Pass a :class:~tralda.visualization.style.TreeStyle to control the appearance of every node.
The style is resolved once per node during :meth:render via
:meth:~tralda.visualization.style.TreeStyle.resolve, which merges the default style, an
optional per-node style function, and optional direct node overrides in that order.
Example usage:
from tralda.visualization.layout import TreeLayout
from tralda.visualization.matplotlib_renderer import MatplotlibRenderer
from tralda.visualization.style import TreeStyle
layout = TreeLayout(tree, edge_length_mode="attr")
style = TreeStyle.from_maps(
symbol_map={"S": "circle", "D": "square", "H": "triangle-up"},
node_color_map={"a": "steelblue", "b": "tomato"},
)
renderer = MatplotlibRenderer(layout, tree_style=style)
fig, ax = renderer.render()
Source code in tralda/visualization/matplotlib_renderer/_renderer.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
__init__(layout: TreeLayout, ax: Axes | None = None, *, tree_style: TreeStyle | None = None, rescale_depth: bool = True, show_labels: bool = True, show_internal_labels: bool = False, show_ghost_segments: bool = True, label_pad: float = 4.0, figsize: tuple[float, float] | None = None) -> None
¶
Construct a renderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout
|
TreeLayout
|
Pre-computed tree layout to render. |
required |
ax
|
Axes | None
|
Existing :class: |
None
|
tree_style
|
TreeStyle | None
|
Styling configuration for nodes, edges, and labels. When |
None
|
rescale_depth
|
bool
|
Normalise the depth axis to |
True
|
show_labels
|
bool
|
Draw leaf labels. Default |
True
|
show_internal_labels
|
bool
|
Also draw labels for internal nodes (including the root). Only
nodes that have a |
False
|
show_ghost_segments
|
bool
|
Extend short leaves to the maximum depth with a dashed line. Only
visible when leaves sit at different depths ( |
True
|
label_pad
|
float
|
Gap between the symbol edge and the start of the label text, in points.
Default |
4.0
|
figsize
|
tuple[float, float] | None
|
Figure size |
None
|
Source code in tralda/visualization/matplotlib_renderer/_renderer.py
render() -> tuple[Figure, Axes]
¶
Draw the tree and return the figure and axes.
When no ax was supplied to the constructor a new figure is created with an automatically
derived size. fig.tight_layout() is called automatically for new figures; when drawing
on a caller-supplied axes the caller is responsible for layout management.
Returns:
| Type | Description |
|---|---|
Figure
|
|
Axes
|
class: |
Source code in tralda/visualization/matplotlib_renderer/_renderer.py
register_symbol(name: str, drawer: SymbolDrawer) -> None
¶
Register a custom symbol drawer in the module-level registry.
Once registered, the symbol is immediately available to all :class:MatplotlibRenderer
instances by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Key under which the drawer is registered. |
required |
drawer
|
SymbolDrawer
|
Callable with the signature
|
required |
Source code in tralda/visualization/matplotlib_renderer/_symbols.py
Plotly backend¶
PlotlyRenderer
¶
Bases: BaseRenderer
Render a :class:~tralda.visualization.layout.TreeLayout as an interactive Plotly figure.
All tree geometry is read from a pre-computed :class:~tralda.visualization.layout.TreeLayout.
Node appearance is controlled by a :class:~tralda.visualization.style.TreeStyle.
Example usage::
from tralda.visualization.layout import TreeLayout
from tralda.visualization.plotly_renderer import PlotlyRenderer
from tralda.visualization.style import TreeStyle
layout = TreeLayout(tree, edge_length_mode="attr")
style = TreeStyle(leaf_symbol="circle")
renderer = PlotlyRenderer(layout, tree_style=style, hover_attrs=["dist", "support"])
fig = renderer.render()
fig.show() # interactive browser / Jupyter
fig.write_html("tree.html") # self-contained HTML file
fig.write_image("tree.png") # static image, requires the 'kaleido' package
# (included in the 'plotly' extra:
# pip install tralda[plotly])
Source code in tralda/visualization/plotly_renderer/_renderer.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
__init__(layout: TreeLayout, *, tree_style: TreeStyle | None = None, rescale_depth: bool = True, show_labels: bool = True, show_internal_labels: bool = False, show_ghost_segments: bool = True, width: int | None = None, height: int | None = None, hover_attrs: list[str] | str | None = None) -> None
¶
Construct a renderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout
|
TreeLayout
|
Pre-computed tree layout to render. |
required |
tree_style
|
TreeStyle | None
|
Styling configuration for nodes, edges, and labels. When |
None
|
rescale_depth
|
bool
|
Normalise the depth axis to |
True
|
show_labels
|
bool
|
Draw leaf labels. Default |
True
|
show_internal_labels
|
bool
|
Also draw labels for internal nodes (including the root). Only
nodes that have a |
False
|
show_ghost_segments
|
bool
|
Extend short leaves to the maximum depth with a dashed line. Only
visible when leaves sit at different depths ( |
True
|
width
|
int | None
|
Figure width in pixels. When |
None
|
height
|
int | None
|
Figure height in pixels. When |
None
|
hover_attrs
|
list[str] | str | None
|
One or more node attribute names to include in hover tooltips in addition
to |
None
|
Source code in tralda/visualization/plotly_renderer/_renderer.py
render() -> go.Figure
¶
Build and return the Plotly figure.
Returns:
| Name | Type | Description |
|---|---|---|
A |
Figure
|
class: |