简体   繁体   English

Biopython Phylo.draw():为什么不返回轴实例?

[英]Biopython Phylo.draw() : Why not returning axes instance?

There is something I don't understand in Bio.Phylo, when we do : 当我们这样做时,Bio.Phylo中有一些我不理解的东西:

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. tree_plot是Nonetype,因为Phylo.draw()不返回任何内容。 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: 现在,用我在开头写的那段代码,我在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. 等等任何其他axis.AxesSubplot方法。

My question is : 我的问题是:

Why is there no return statement in the Phylo.draw() function ? 为什么Phylo.draw()函数中没有return语句? If it's on purpose, why and how one can draw something on the same axis ? 如果它是有目的的,为什么以及如何在同一轴上绘制东西?

I'm using 我正在使用

  • python 2.7.3 python 2.7.3
  • matplotlib 1.3.0 matplotlib 1.3.0
  • Biopython 1.63 Biopython 1.63

Thanks 谢谢

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: 首先确保您在交互模式下使用matplotlib

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 ,添加一些你使用的文字

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

Or if you want the whole figure use plt.gcf() . 或者如果你想要整个数字使用plt.gcf() Remember to do plt.show() to see your updates. 记得做plt.show()来查看你的更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM