title: Aligning dendrogram with heat map using matplotlib
body: <p>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 href="http://digitheadslabnotebook.blogspot.com/2011/06/drawing-heatmaps-in-r.html" rel="nofollow noreferrer">a blog</a>:<a href="https://i.stack.imgur.com/vTFRc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vTFRc.png" alt="enter image description here"></a></p>
  
  <p>Right now I have a python script that looks like this:</p>
  
  <pre><code>%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)
  </code></pre>
  
  <p>And it the figure generated from this is shown as follows:
  <a href="https://i.stack.imgur.com/tnYKV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tnYKV.png" alt="enter image description here"></a></p>
  
  <p>I would like to align the y axis of the heat map with the x axis of the dendrogram. Any suggestions?</p>