2

I would like to generate a figure that combines a dendrogram and heatmap. The package in R does this very well but I cannot find a way to do so in python. Here is an example from a blog:enter image description here

Right now I have a python script that looks like this:

%matplotlib inline
from sklearn.metrics.pairwise import pairwise_distances
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage
from scipy.cluster.hierarchy import dendrogram
import numpy as np
import matplotlib.pyplot as plt

X = np.random.rand(4, 8)

Z = linkage(squareform(np.around(pairwise_distances(X), 4)))

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
dendrogram(Z, ax=ax1)
ax2.pcolor(X, cmap=plt.cm.Reds)

And it the figure generated from this is shown as follows: enter image description here

I would like to align the y axis of the heat map with the x axis of the dendrogram. Any suggestions?

Xiangyu
  • 824
  • 9
  • 34
  • 1
    Steve Tjoa has a good post on this: http://stackoverflow.com/questions/2455761/reordering-matrix-elements-to-reflect-column-and-row-clustering-in-naiive-python/3017704#3017704 – nv_wu Aug 05 '15 at 20:12
  • Thank you, this is exactly what I want. – Xiangyu Aug 06 '15 at 19:52

0 Answers0