.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/tree/plot_trees_of_trees.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_trees_of_trees.py: Tree of trees ============= This example shows how to plot trees at the leaf of a tree, using the `iplotx` library. Note that there are a few conceivable ways to do similar things as this in `matplotlib` and this is just one such approach, using inset axes. .. GENERATED FROM PYTHON SOURCE LINES 9-54 .. code-block:: Python from ete4 import Tree import matplotlib.pyplot as plt import iplotx as ipx tree = Tree( "(,((),(((),()),((),()))));", ) subtree_strings = [ "((a,b),(c,d));", "(a,((b,c),d));", "(a,b,c,d);", "(a,(b,(c,d)));", "(a,b);", "(a,(b,c,d));", ] # Plot the initial tree fig, ax = plt.subplots(figsize=(8, 5)) art = ipx.tree( tree, ax=ax, aspect=1, edge_color="grey", ) for i, leaf in enumerate(tree.leaves()): # Create an axis in the rough proximity # and with the rough diemension of a leaf, # with some padding x, y = art.get_layout().T[leaf].values subax = ax.inset_axes( (x + 0.1, y - 0.4, 1.2, 0.8), transform=ax.transData, ) # For each leaf, make a tree subtree = Tree(subtree_strings[i]) # Plot the tree with dashed lines ipx.tree( subtree, ax=subax, edge_linestyle="--", ) .. image-sg:: /gallery/tree/images/sphx_glr_plot_trees_of_trees_001.png :alt: plot trees of trees :srcset: /gallery/tree/images/sphx_glr_plot_trees_of_trees_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 55-56 Combining this with other elements, e.g. cascading backgrounds, is possible too: .. GENERATED FROM PYTHON SOURCE LINES 56-83 .. code-block:: Python fig, ax = plt.subplots(figsize=(8, 5)) art = ipx.tree( tree, ax=ax, aspect=1, edge_color="grey", cascade_facecolor={ list(tree.leaves())[0]: "lightblue", list(tree.traverse("preorder"))[2]: "pink", }, ) for i, leaf in enumerate(tree.leaves()): x, y = art.get_layout().T[leaf].values subax = ax.inset_axes( (x + 0.1, y - 0.4, 1.2, 0.8), transform=ax.transData, ) # Set the inset axes background transparent subax.patch.set_alpha(0) subtree = Tree(subtree_strings[i]) ipx.tree( subtree, ax=subax, edge_linestyle="--", ) .. image-sg:: /gallery/tree/images/sphx_glr_plot_trees_of_trees_002.png :alt: plot trees of trees :srcset: /gallery/tree/images/sphx_glr_plot_trees_of_trees_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.295 seconds) .. _sphx_glr_download_gallery_tree_plot_trees_of_trees.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_trees_of_trees.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_trees_of_trees.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_trees_of_trees.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_