title: How to plot contourf colorbar in different subplot - matplotlib
body: <p>This is a very similar question to <a href="https://stackoverflow.com/questions/18874135/how-to-plot-pcolor-colorbar-in-a-different-subplot-matplotlib">"How to plot pcolor colorbar in a different subplot - matplotlib"</a>. I am trying to plot a filled contour plot and a line plot with a shared axis and the colorbar in a separate subplot (i.e. so it doesn't take up space for the contourf axis and thus muck up the x-axis sharing). However, the x-axis in my code does not rescale nicely:</p>
  
  <pre><code>import numpy as np
  import matplotlib as mpl
  import matplotlib.pyplot as plt
  
  z = np.random.rand(20, 20)
  x, y = np.arange(20), np.arange(20)
  y2 = np.random.rand(20)
  fig = plt.figure(figsize=(8, 8))
  gs = mpl.gridspec.GridSpec(2, 2, height_ratios=[1, 2], width_ratios=[2, 1])
  ax1 = fig.add_subplot(gs[1, 0])
  ax2 = fig.add_subplot(gs[0, 0], sharex=ax1)
  ax3 = fig.add_subplot(gs[1, 1])
  cont = ax1.contourf(x, y, z, 20)
  plt.tick_params(which='both', top=False, right=False)
  ax2.plot(x, y2, color='g')
  plt.tick_params(which='both', top=False, right=False)
  cbar = plt.colorbar(cont, cax=ax3)
  cbar.set_label('Intensity', rotation=270, labelpad=20)
  plt.tight_layout()
  plt.show()
  </code></pre>
  
  <p>which produces an x-axis scaled from 0 to 20 (inclusive) rather than 0 to 19, which means there is unsightly whitespace in the filled contour plot. Commenting out the <code>sharex=ax1</code> in the above code means that the x-axis for the contour plot is scaled nicely, but not for the line plot above it and the <code>plt.tick_params</code> code has no effect on either axis.</p>
  
  <p><a href="https://i.stack.imgur.com/mx7tZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mx7tZ.png" alt="plot"></a></p>
  
  <p>Is there a way of solving this?</p>