.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/tree/plot_tree_node_background.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_tree_plot_tree_node_background.py: Tree cascades ============= This example shows how to use `iplotx` to add cascading backgrounds to trees. "Cascading" here means that each patch (rectangle/wedge/etc.) will cover a node and all descendants, down to the leaves. .. GENERATED FROM PYTHON SOURCE LINES 9-35 .. code-block:: Python from Bio import Phylo from io import StringIO import matplotlib.pyplot as plt import iplotx as ipx # Make a tree from a string in Newick format tree = next( Phylo.NewickIO.parse( StringIO( "(()(()((()())(()()))))", ) ) ) backgrounds = { tree.get_nonterminals()[3]: "turquoise", tree.get_terminals()[0]: "tomato", tree.get_terminals()[1]: "purple", } ipx.plotting.tree( tree, cascade_facecolor=backgrounds, ) .. image-sg:: /gallery/tree/images/sphx_glr_plot_tree_node_background_001.png :alt: plot tree node background :srcset: /gallery/tree/images/sphx_glr_plot_tree_node_background_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 36-38 Cascading patches have a style option "extend" which affects whether the patches extend to the end of the deepest leaf: .. GENERATED FROM PYTHON SOURCE LINES 38-46 .. code-block:: Python ipx.plotting.tree( tree, layout="vertical", cascade_facecolor=backgrounds, cascade_extend=True, ) .. image-sg:: /gallery/tree/images/sphx_glr_plot_tree_node_background_002.png :alt: plot tree node background :srcset: /gallery/tree/images/sphx_glr_plot_tree_node_background_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 47-48 Cascading patches work with radial layouts as well: .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: Python # sphinx_gallery_thumbnail_number = 3 ipx.plotting.tree( tree, layout="radial", cascade_facecolor=backgrounds, cascade_extend=True, aspect=1, ) .. image-sg:: /gallery/tree/images/sphx_glr_plot_tree_node_background_003.png :alt: plot tree node background :srcset: /gallery/tree/images/sphx_glr_plot_tree_node_background_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 59-61 Cascading patches exclude leaf labels by default, but can be extended to cover leaf labels by setting the parameter as follows: .. GENERATED FROM PYTHON SOURCE LINES 61-86 .. code-block:: Python fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5)) ipx.plotting.tree( tree, ax=ax1, layout="radial", leaf_labels={leaf: f"L{i + 1}" for i, leaf in enumerate(tree.get_terminals())}, cascade_facecolor=backgrounds, cascade_extend=True, # Exclude leaf labels aspect=1, margins=0.4, title="Exclude leaf labels", ) ipx.plotting.tree( tree, ax=ax2, layout="radial", leaf_labels={leaf: f"L{i + 1}" for i, leaf in enumerate(tree.get_terminals())}, cascade_facecolor=backgrounds, cascade_extend="leaf_labels", # Include leaf labels aspect=1, margins=0.4, title="Include leaf labels", ) .. image-sg:: /gallery/tree/images/sphx_glr_plot_tree_node_background_004.png :alt: Exclude leaf labels, Include leaf labels :srcset: /gallery/tree/images/sphx_glr_plot_tree_node_background_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 87-88 This extension also works with other layouts, such as horizontal and vertical: .. GENERATED FROM PYTHON SOURCE LINES 88-110 .. code-block:: Python layout_and_orientations = { "horizontal": ["left", "right"], "vertical": ["descending", "ascending"], "radial": ["clockwise", "counterclockwise"], } fig, axs = plt.subplots(3, 2, figsize=(9, 13.5)) for i, (layout_name, layout_orientations) in enumerate(layout_and_orientations.items()): for j, orientation in enumerate(layout_orientations): ipx.plotting.tree( tree, ax=axs[i, j], layout=layout_name, layout_orientation=orientation, leaf_labels={leaf: f"L{i + 1}" for i, leaf in enumerate(tree.get_terminals())}, cascade_facecolor=backgrounds, cascade_extend="leaf_labels", aspect=1, title=f"{layout_name} ({orientation})", margins=0.1, ) .. image-sg:: /gallery/tree/images/sphx_glr_plot_tree_node_background_005.png :alt: horizontal (left), horizontal (right), vertical (descending), vertical (ascending), radial (clockwise), radial (counterclockwise) :srcset: /gallery/tree/images/sphx_glr_plot_tree_node_background_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.679 seconds) .. _sphx_glr_download_gallery_tree_plot_tree_node_background.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_tree_node_background.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_tree_node_background.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_tree_node_background.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_