change the font size of leaf nodes when generating phylogenetic trees using Bio.Phylo.draw()

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 cStringIO import StringIO

def plot_tree(treedata, output_file):
    handle = StringIO(treedata)  # parse the newick string
    tree =, "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)


#!/usr/bin/env python

from Bio import Phylo
from cStringIO import StringIO
from matplotlib import pyplot as plt
import csv
import argparse
import os
import matplotlib

import matplotlib.font_manager as font_manager

colors = dict(Expansions='green', Contractions='red', Rapid='blue')

labels = dict()

def display(tree_type):
 if tree_type == 'Rapid':
 return 'Rapidly evolving families'

 return tree_type

def label(n):
 family_count =
 return "%s (%s)" % (, family_count)

 return family_count

def prettify_tree(fig):
 axis='y', # changes apply to the y-axis
 which='both', # both major and minor ticks are affected
 left='off', # ticks along the left edge are off
 right='off', # ticks along the right edge are off
 labelleft='off') # labels along the left edge are off

 [fig.gca().spines[border].set_visible(False) for border in ['left', 'right', 'top']]

def draw_tree(datafile, tree_type, newick, node_ids, output_file):
 with open(datafile) as f:
 reader = csv.DictReader(f, delimiter='\t')

 for row in reader:
 labels[row["Node"]] = row

 tree =, "newick")
 id_tree =, "newick")

 for clade, c_id in zip(tree.find_clades(), id_tree.find_clades()): = labels[][display(tree_type)] if in labels else ""

 tree.ladderize() # Flip branches so deeper clades are displayed at top


 fig = plt.figure(frameon=False)
 matplotlib.rc('font', size=6)
 Phylo.draw(tree, axes=fig.gca(), do_show=False, label_func=label, label_colors = lambda n: colors[tree_type])
 plt.title(display(tree_type) + " (count)")

 if output_file:
 fig.savefig(output_file, format='png', bbox_inches='tight', pad_inches=0, dpi=180)


if __name__ == "__main__":
 parser = argparse.ArgumentParser(description=__doc__, prog="")
 parser.add_argument("-i", "--input-file", action="store", dest="input_file", required=True, type=str, help="full path to mcl's output dump file")
 parser.add_argument("-t", "--tree", action="store", dest="tree", required=True, help="Newick tree to display")
 parser.add_argument("-d", "--ids", action="store", dest="id_tree", required=True, help="Matching Newick tree with node IDs")
 parser.add_argument("-y", "--tree-type", action="store", dest="tree_type", default="Expansions", required=False, type=str, help="Expansions, Contractions, or Rapid")
 parser.add_argument("-o", "--output-file", action="store", dest="output_file", required=False, type=str, help="output PNG file name")

 args = parser.parse_args()

 if not os.path.isfile(args.input_file):
 exit("Could not find input file. Exiting...\n")

 draw_tree(args.input_file, args.tree_type, args.tree, args.id_tree, args.output_file)

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Protected by WP Anti Spam