Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With SciPy dendrogram, can I change the linewidth?

I'm making a big dendrogram using SciPy and in the resulting dendrogram the line thickness makes it hard to see detail. I want to decrease the line thickness to make it easier to see and more MatLab like. Any suggestions?

I'm doing:

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

And getting a result like this.

like image 929
ja.kb.ca Avatar asked Mar 31 '14 22:03

ja.kb.ca


2 Answers

Matplotlib has a context manager now, which allows you to only override the default values temporarily, for that one plot:

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

See the Matplotlib configuration API for more details.

like image 65
Juan Avatar answered Sep 27 '22 16:09

Juan


Set the default linewidth before calling dendrogram. For example:

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

See Customizing matplotlib for more information.

like image 38
Warren Weckesser Avatar answered Sep 27 '22 17:09

Warren Weckesser