title: Plotting two heat maps side by side in Matplotlib
body: <p>I have a function that plots the heat map for the correlation matrix of a DataFrame. The function looks like this:</p>
  
  <pre><code>def corr_heatmap(data):
      columns = data.columns
      corr_matrix = data.corr()
  
      fig, ax = plt.subplots(figsize=(7, 7))
      mat = ax.matshow(corr_matrix, cmap='coolwarm')
  
      ax.set_xticks(range(len(columns)))
      ax.set_yticks(range(len(columns)))
      ax.set_xticklabels(columns)
      ax.set_yticklabels(columns)
      plt.setp(ax.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
      plt.colorbar(mat, fraction=0.045, pad=0.05)
      fig.tight_layout()
      plt.show()
  
      return mat
  </code></pre>
  
  <p>and when run with a DataFrame outputs something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/YvY3z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YvY3z.png" alt="enter image description here"></a></p>
  
  <p>What I want to do is plot two of these heat maps side by side, but I'm having some trouble doing so. What I've done so far is attempt to assign each heat map to an AxesImage object and use subplots to plot them.</p>
  
  <pre><code>mat1 = corr_heatmap(corr_mat1)
  mat2 = corr_heatmap(corr_mat2)
  
  fig = plt.figure(figsize=(15, 15))
  ax1 = fig.add_subplot(221)
  ax2 = fig.add_subplot(222)
  ax1.plot(ma1)
  ax2.plot(ma2)
  </code></pre>
  
  <p>but this gives me the following error:</p>
  
  <pre><code>TypeError: float() argument must be a string or a number, not 'AxesImage'
  </code></pre>
  
  <p>Would anybody happen to know a way that I could plot two heat map images side by side? Thank you.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>In case anyone's wondering what the final code for what I wanted to do would look like:</p>
  
  <pre><code>def corr_heatmaps(data1, data2, method='pearson'):
  
      # Basic Configuration
      fig, axes = plt.subplots(ncols=2, figsize=(12, 12))
      ax1, ax2 = axes
      corr_matrix1 = data1.corr(method=method)
      corr_matrix2 = data2.corr(method=method)
      columns1 = corr_matrix1.columns
      columns2 = corr_matrix2.columns
  
      # Heat maps.
      im1 = ax1.matshow(corr_matrix1, cmap='coolwarm')
      im2 = ax2.matshow(corr_matrix2, cmap='coolwarm')
  
      # Formatting for heat map 1.
      ax1.set_xticks(range(len(columns1)))
      ax1.set_yticks(range(len(columns1)))
      ax1.set_xticklabels(columns1)
      ax1.set_yticklabels(columns1)
      ax1.set_title(data1.name, y=-0.1)
      plt.setp(ax1.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
      plt.colorbar(im1, fraction=0.045, pad=0.05, ax=ax1)
  
      # Formatting for heat map 2.
      ax2.set_xticks(range(len(columns2)))
      ax2.set_yticks(range(len(columns2)))
      ax2.set_xticklabels(columns2)
      ax2.set_yticklabels(columns2)
      ax2.set_title(data2.name, y=-0.1)
      plt.setp(ax2.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
      plt.colorbar(im2, fraction=0.045, pad=0.05, ax=ax2)
  
      fig.tight_layout()
  </code></pre>
  
  <p>This could (when run with two Pandas DataFrames) outputs something along the following image:</p>
  
  <p><a href="https://i.stack.imgur.com/m3h3F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m3h3F.png" alt="enter image description here"></a></p>