【问题标题】:With SciPy dendrogram, can I change the linewidth?使用 SciPy 树状图,我可以更改线宽吗?
【发布时间】:2014-05-11 11:55:39
【问题描述】:

我正在使用 SciPy 制作一个大树状图,在生成的树状图中,线条粗细让人难以看到细节。我想减少线条的粗细,使其更容易看到并且更像 MatLab。有什么建议吗?

我在做:

import scipy.cluster.hierarchy as hicl
from pylab import savefig

distance = #distance matrix

links = hicl.linkage(distance,method='average')
pden = hicl.dendrogram(links,color_threshold=optcutoff[0], ...
       count_sort=True,no_labels=True)
savefig('foo.pdf')

得到类似this的结果。

【问题讨论】:

    标签: matplotlib scipy hierarchical-clustering


    【解决方案1】:

    在调用dendrogram 之前设置默认线宽。例如:

    import scipy.cluster.hierarchy as hicl
    from pylab import savefig
    import matplotlib
    
    
    # Override the default linewidth.
    matplotlib.rcParams['lines.linewidth'] = 0.5
    
    distance = #distance matrix
    
    links = hicl.linkage(distance,method='average')
    pden = hicl.dendrogram(links,color_threshold=optcutoff[0], ...
           count_sort=True,no_labels=True)
    savefig('foo.pdf')
    

    更多信息请参见Customizing matplotlib

    【讨论】:

    • 这可以在不导入 matplotlib 和导入 pyplot 的情况下完成吗?
    【解决方案2】:

    Matplotlib 现在有一个上下文管理器,它只允许您临时覆盖该图的默认值:

    import matplotlib.pyplot as plt
    from scipy.cluster import hierarchy
    
    distance = #distance matrix
    links = hierarchy.linkage(distance, method='average')
    
    # Temporarily override the default line width:
    with plt.rc_context({'lines.linewidth': 0.5}):
        pden = hierarchy.dendrogram(links, color_threshold=optcutoff[0], ...
                                    count_sort=True, no_labels=True)
    # linewidth is back to its default here...!
    plt.savefig('foo.pdf')
    

    有关详细信息,请参阅Matplotlib configuration API

    【讨论】:

    • 这在许多其他情况下都非常有用!非常整洁。
    猜你喜欢
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2016-03-14
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多