title: Matplotlib: Add colorbar to non-mappable object
body: <p>I have a series of lines representing the change of a variable; each with a unique color. For that reason I want to add a colorbar next to the plot. The desired output is shown below. </p>
  
  <p>The problem is that <code>plot</code> is a non-mappable object, i.e. the colorbar has to be added manually. I consider my current solution (below) sub-optimal as it involves size parameters of which I have no interest in controlling. I'd prefer a similar solution as for a mappable object (example below current solution).</p>
  
  <h2>Desired output</h2>
  
  <p><a href="https://i.stack.imgur.com/FDWkz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FDWkz.png" alt="Desired output"></a></p>
  
  <h2>Current solution</h2>
  
  <pre><code>import numpy             as np
  import matplotlib        as mpl
  import matplotlib.pyplot as plt
  
  x    = np.linspace(0, 5, 100)
  N    = 20
  cmap = plt.get_cmap('jet',N)
  
  fig  = plt.figure(figsize=(8,6))
  ax1  = fig.add_axes([0.10,0.10,0.70,0.85])
  
  for i,n in enumerate(np.linspace(0,2,N)):
      y = np.sin(x)*x**n
      ax1.plot(x,y,c=cmap(i))
  
  plt.xlabel('x')
  plt.ylabel('y')
  
  ax2  = fig.add_axes([0.85,0.10,0.05,0.85])
  norm = mpl.colors.Normalize(vmin=0,vmax=2)
  cb1  = mpl.colorbar.ColorbarBase(ax2,cmap=cmap,norm=norm,orientation='vertical')
  
  plt.show()
  </code></pre>
  
  <h2>Desired solution</h2>
  
  <p>(obviously replacing <code>imshow</code>)</p>
  
  <pre><code>fig,ax = plt.subplots()
  cax    = ax.imshow(..)
  cbar   = fig.colorbar(cax,aspect=10)
  plt.show()
  </code></pre>