title: Add colorbar to existing axis
body: <p>I'm making some interactive plots and I would like to add a colorbar legend. I don't want the colorbar to be in its own axes, so I want to add it to the existing axes. I'm having difficulties doing this, as most of the example code I have found creates a new axes for the colorbar. </p>
  
  <p>I have tried the following code using <code>matplotlib.colorbar.ColorbarBase</code>, which adds a colorbar to an existing axes, but it gives me strange results and I can't figure out how to specify attributes of the colorbar (for instance, where on the axes it is placed and what size it is)</p>
  
  <pre><code>import matplotlib
  import matplotlib.pyplot as plt
  from matplotlib.cm import coolwarm
  import numpy as np
  
  x = np.random.uniform(1, 10, 10)
  y = np.random.uniform(1, 10, 10)
  v = np.random.uniform(1, 10, 10)
  
  fig, ax = plt.subplots()
  
  s = ax.scatter(x, y, c=v, cmap=coolwarm)
  
  matplotlib.colorbar.ColorbarBase(ax=ax, cmap=coolwarm, values=sorted(v),
                                   orientation="horizontal")
  </code></pre>
  
  <p>Using <code>fig.colorbar</code> instead of<code>matplotlib.colorbar.ColorbarBase</code> still doesn't give me quite what I want, and I still don't know how to adjust the attributes of the colorbar.</p>
  
  <pre><code>fig.colorbar(s, ax=ax, cax=ax)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/UyVAf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/UyVAf.png" alt="enter image description here"></a></p>
  
  <p>Let's say I want to have the colorbar in the top left corner, stretching about halfway across the top of the plot. How would I go about doing that?</p>
  
  <p>Am I better off writing a custom function for this, maybe using <code>LineCollection</code>?</p>