Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 441
madcap Avatar asked Apr 02 '15 18:04

madcap


People also ask

What is the purpose of the phylogenetic tree?

A phylogenetic tree is a diagram that represents evolutionary relationships among organisms. Phylogenetic trees are hypotheses, not definitive facts. The pattern of branching in a phylogenetic tree reflects how species or other groups evolved from a series of common ancestors.

How to change the fontsize of scientific notation in Matplotlib?

To change the fontsize of scientific notation in matplotlib, we can take the following steps − Set the figure size and adjust the padding between and around the subplots. Make a list of x and y values. Plot x and y data points using plot () method. To change the font size of scientific notation, we can use style="sci" class by name.

How do you get all leaf nodes in Python?

In python you can use an internal function to collect the leaf nodes and then return the list of them. def get_leaf_nodes (self): leafs = [] def _get_leaf_nodes (node): if node is not None: if len (node.children) == 0: leafs.append (node) for n in node.children: _get_leaf_nodes (n) _get_leaf_nodes (self.root) return leafs

What is the default font size for all elements in Matplotlib?

Note: The default font size for all elements is 10. The following code shows how to change the font size of every element in the plot: You can find more Matplotlib tutorials here.

How to change the font size of a line plot?

Let’s start with creating a line plot with the current settings. The font size is too small as we can barely read the axis titles. 1. Set_theme function The first method for changing the font size is to use the set_theme function.


1 Answers

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
like image 89
Fábio Madeira Avatar answered Nov 01 '22 08:11

Fábio Madeira