How can I plot a dendrogram right on top of a matrix of values, reordered appropriately to reflect the clustering, in Python? An example is the following figure:
This is Figure 6 from: A panel of induced pluripotent stem cells from chimpanzees: a resource for comparative functional genomics
I use scipy.cluster.dendrogram to make my dendrogram and perform hierarchical clustering on a matrix of data. How can I then plot the data as a matrix where the rows have been reordered to reflect a clustering induced by the cutting the dendrogram at a particular threshold, and have the dendrogram plotted alongside the matrix? I know how to plot the dendrogram in scipy, but not how to plot the intensity matrix of data with the right scale bar next to it.

The question does not define matrix very well: "matrix of values", "matrix of data". I assume that you mean a distance matrix. In other words, element D_ij in the symmetric nonnegative N-by-N distance matrix D denotes the distance between two feature vectors, x_i and x_j. Is that correct?
If so, then try this (edited June 13, 2010, to reflect two different dendrograms).
Tested in
python 3.10andmatplotlib 3.5.1Edit: For different colors, adjust the
cmapattribute inimshow. See the scipy/matplotlib docs for examples. That page also describes how to create your own colormap. For convenience, I recommend using a preexisting colormap. In my example, I usedYlGnBu.Edit:
add_axes(see documentation here) accepts a list or tuple:(left, bottom, width, height). For example,(0.5,0,0.5,1)adds anAxeson the right half of the figure.(0,0.5,1,0.5)adds anAxeson the top half of the figure.Most people probably use
add_subplotfor its convenience. I likeadd_axesfor its control.To remove the border, use
add_axes([left,bottom,width,height], frame_on=False). See example here.