簡體   English   中英

在 python 中,如何在使用 Bio.Phylo.draw() 生成系統發育樹時更改葉節點的字體大小?

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

我正在使用 Biopython 的 Phylo 包來創建系統發育樹。

對於大樹,我需要減小葉節點的字體大小。 有人建議更改 matplotlib.pyplot.rcParams['font.size'] 但這僅允許我更改軸名稱和標題,因為 Phylo 定義了自己的字體大小。 我無法更改 Phylo 源代碼,因為我在大學使用它。 定義圖形或軸不是一個選項,因為 Phylo.draw() 創建了自己的選項。

有沒有人對如何解決這個問題有任何建議,也許是拉伸 y 軸?

到目前為止,我使用以下代碼來生成樹:

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

Phylo.draw()可以將軸作為參數。 從Biopython中的方法文檔中,您可以閱讀以下內容

axes:matplotlib / pylab axes如果是有效的matplotlib.axes.Axes實例,則在該Axes中繪制該phylogram。 默認情況下(無),將創建一個新圖形。

這意味着您可以根據自己的尺寸加載自己的軸。 例如

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)
    # 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

如果這對您有用,請告訴我。 法比奧

當我拍攝大量序列時,我遇到了同樣的問題,比如 50 個樹分支相互沖突,如何設置這些對我有幫助,任何人。 我還想設置節點字體大小。 這是我的代碼:

def plot_tree(support_tree, output_file):
 Phylo.draw_ascii(support_tree)
 fig1 = plt.gcf()
 pylab.axis('off')
 plt.tight_layout()
 plt.savefig('Tree.png', format='png', bbox_inches='tight', dpi=700)
retrun

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM