Domanda

There is something I don't understand in Bio.Phylo, when we do :

from Bio import Phylo
tree = Phylo.read("my_tree","newick")
tree_plot = Phylo.draw(tree)

EDIT :

$ cat my_tree
(((A,B),C),D)

tree_plot is Nonetype because Phylo.draw() doesn't return anything. The 2 last lines of the source code are :

    if do_show:
        plt.show()

I would like to interact with the tree, and add for instance a dot in front of some of the tree leaves. So I added in the source code on my computer at the last line :

    if do_show:
        plt.show()
    return axes

now, with the piece of code I write at the beginning, I have in tree_plot:

In [1]: type(tree_plot)
Out[1]: matplotlib.axes.AxesSubplot

And I'm now able to access data from the tree with for instance :

In [2]: tree_plot.texts
Out[2]: [<matplotlib.text.Text at 0x114d02710>,
<matplotlib.text.Text at 0x114d02990>,
<matplotlib.text.Text at 0x114d02ed0>,
<matplotlib.text.Text at 0x114d00cd0>,
<matplotlib.text.Text at 0x114d3c410>,
<matplotlib.text.Text at 0x114d3cb10>] 

or plotting a line with :

tree_plot.plot(range(10))

And so on with any other axes.AxesSubplot method.

My question is :

Why is there no return statement in the Phylo.draw() function ? If it's on purpose, why and how one can draw something on the same axis ?

I'm using

  • python 2.7.3
  • matplotlib 1.3.0
  • Biopython 1.63

Thanks

È stato utile?

Soluzione

Why they don't return the figure you will have to ask the developers (or maybe suggest the change). However it is quite easy to work around:

First make sure you have matplotlib in interactive mode:

from matplotlib import pyplot as plt
plt.ion()

Then run your:

Phylo.draw(tree)

After that if you just want the ax, to add some text you use

ax = plt.gca()
ax.text(1, 4, "Hello World")
plt.show()

Or if you want the whole figure use plt.gcf(). Remember to do plt.show() to see your updates.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top