.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/tree/plot_biopython_tree.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_biopython_tree.py: Biopython tree ============== This example shows how to use `iplotx` to plot trees from Biopython's Phylo module. .. GENERATED FROM PYTHON SOURCE LINES 7-26 .. 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( "(()(()((()())(()()))))", ) ) ) ipx.plotting.tree( tree, ) .. image-sg:: /gallery/tree/images/sphx_glr_plot_biopython_tree_001.png :alt: plot biopython tree :srcset: /gallery/tree/images/sphx_glr_plot_biopython_tree_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 27-30 To add labels to the leaves, you can use the `leaf_labels` argument as a dictionary. Moreover, the plot can be customised further using hmargin (horizontal label margin) and some `matplotlib` settings. .. GENERATED FROM PYTHON SOURCE LINES 30-44 .. code-block:: Python # sphinx_gallery_thumbnail_number = 2 leaf_labels = {leaf: f"Species {i + 1}" for i, leaf in enumerate(tree.get_terminals())} fig, ax = plt.subplots(figsize=(4, 4)) ipx.plotting.tree( tree, ax=ax, leaf_labels=leaf_labels, leaf_label_hmargin=10, leaf_deep=False, ) ax.set_xlim(0, 8) ax.invert_yaxis() .. image-sg:: /gallery/tree/images/sphx_glr_plot_biopython_tree_002.png :alt: plot biopython tree :srcset: /gallery/tree/images/sphx_glr_plot_biopython_tree_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-47 `iplotx` can compute a radial tree layout as well, and usual style modifications work for trees same as networks: .. GENERATED FROM PYTHON SOURCE LINES 47-63 .. code-block:: Python ipx.plotting.tree( tree, layout="radial", style=[ "tree", { "edge": { "color": ["black", "pink"], "linewidth": 4, }, }, ], aspect=1, title="Black pink in your area", ) .. image-sg:: /gallery/tree/images/sphx_glr_plot_biopython_tree_003.png :alt: Black pink in your area :srcset: /gallery/tree/images/sphx_glr_plot_biopython_tree_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.109 seconds) .. _sphx_glr_download_gallery_tree_plot_biopython_tree.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_biopython_tree.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_biopython_tree.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_biopython_tree.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_