使用SciPy树形图,我可以更改线宽吗?

时间:2014-03-31 22:35:04

标签: matplotlib scipy hierarchical-clustering

我使用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之类的结果。

2 个答案:

答案 0 :(得分:6)

在调用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

答案 1 :(得分:6)

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