In python, how can I change the font size of leaf nodes when generating phylogenetic trees using Bio.Phylo.draw()?
Asked Answered
G

1

7

I am using the Phylo package from Biopython to create phylogenetic trees.

For big trees, I need to decrease the fontsize of the leaf nodes. It has been suggested to change matplotlib.pyplot.rcParams['font.size'] but this only allows me to change axes names and titles, as Phylo defines its own font sizes. I can not change Phylo source code as I am using it at the University. Defining figures or axes is not an option as Phylo.draw() creates its own.

Does anyone have any suggestions on how to solve the problem, maybe stretching the y-axis?

So far I was using the following code to produce the tree:

import matplotlib
import matplotlib.pyplot as plt
from Bio import Phylo
from cStringIO import StringIO

def plot_tree(treedata, output_file):

    handle = StringIO(treedata) # parse the newick string
    tree = Phylo.read(handle, "newick")
    matplotlib.rc('font', size=6)
    Phylo.draw(tree)
    plt.savefig(output_file)

    return

Plot

Garboil answered 2/4, 2015 at 18:30 Comment(0)
T
5

Phylo.draw() can take the axes as an argument. From the method's documentation in Biopython you can read the following

axes : matplotlib/pylab axes If a valid matplotlib.axes.Axes instance, the phylogram is plotted in that Axes. By default (None), a new figure is created.

This means that you can load your own axes with your size of choice. For example:

import matplotlib
import matplotlib.pyplot as plt
from Bio import Phylo
from io import StringIO

def plot_tree(treedata, output_file):
    handle = StringIO(treedata)  # parse the newick string
    tree = Phylo.read(handle, "newick")
    matplotlib.rc('font', size=6)
    # set the size of the figure
    fig = plt.figure(figsize=(10, 20), dpi=100)
    # alternatively
    # fig.set_size_inches(10, 20)
    axes = fig.add_subplot(1, 1, 1)
    Phylo.draw(tree, axes=axes)
    plt.savefig(output_file, dpi=100)

    return
Trenatrenail answered 7/4, 2015 at 15:46 Comment(4)
Perfect, thank you very much, this is exactly what I wanted. Another thing that you may also know is how to extend the maximum number of characters of the leaf nodes. As you can see, some names are abbreviated and completed with '...'Garboil
@Garboil As you did for the font size try something that deals with the line width (rcParams["lines.linewidth"]). It might work. If not let me know that I will look further. Can you somehow send me your example tree so that I can actually play with it? FábioOlfe
Try the following part of the tree where five of the leaf node names are too long and are not displayed completely.Garboil
treedata = '((histone H3.2 Mus musculus:0.0,PREDICTED histone H3.2-like isoform 1 Nomascus leucogenys:1.0):1.0,(Anopheles gambiae str. PEST AGAP012709-PA Anopheles gambiae str. PEST:1.0,52 PREDICTED histone H3.2 Danio rerio:1.0):1.0,(PREDICTED histone H3 embryonic-like Strongylocentrotus purpuratus:2.0,GM19740 Drosophila sechellia:1.0):1.0,(hypothetical protein BRAFLDRAFT_108838 Branchiostoma floridae:1.0,PREDICTED histone H3.2-like Ornithorhynchus anatinus:1.0):1.0,(GM13629 Drosophila sechellia:1.0,PREDICTED histone H3.2 Bos taurus:1.0):1.0)'Garboil

© 2022 - 2024 — McMap. All rights reserved.