title: Add colorbar as legend to matplotlib scatterplot (multiple subplots, multiple scatters)
body: <p>I have several subplots to which I want to add a single colorbar. Each subplot consists of 7 scatters. I found advise on how to add colorbars, but they are mostly related to the value of each scatter-point and not to the row itself.</p>
  <p>Representative sample code:</p>
  <pre><code>import numpy as np
  from matplotlib import pyplot as plt
  
  x = range(50)
  scales = np.linspace(0, 2, 7)
  locs = range(4)
  cmap = plt.get_cmap(&quot;Spectral&quot;)
  for s_plot in range(4):
      plt.subplot(2, 2, s_plot+1)
      color = iter(cmap(np.linspace(0, 1, len(scales))))
      for scale in scales:
          c = next(color)
          y = np.random.normal(loc=locs[s_plot], scale=scale, size=50)
          plt.scatter(x, y, c=c, s=5)
          plt.title(&quot;Mean = {:d}&quot;.format(locs[s_plot]))
  plt.subplots_adjust(hspace=0.4)
  plt.show()
  </code></pre>
  <p>The above example gives:
  <a href="https://i.stack.imgur.com/czxaK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/czxaK.png" alt="enter image description here" /></a></p>
  <p>My desired colorbar looks like this (fake, to be placed next to the plot):</p>
  <p><a href="https://i.stack.imgur.com/i8QfY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i8QfY.png" alt="enter image description here" /></a></p>
  <p>So the colorbar does not depict the value of my scatterpoints, but rather the different &quot;rows&quot; (in this case: different scales) that are iterated through. In the example that would help match the points to the scales.</p>
  <p>What I tried is a simple</p>
  <pre><code>plt.colorbar()
  </code></pre>
  <p>which is called once after finishing each subplot. But I get
  <code>TypeError: You must first set_array for mappable</code>
  Also, since it is the different scales I want to create the colormap for, I also tried</p>
  <pre><code>plt.colorbar(scales) 
  </code></pre>
  <p>which returns: <code>AttributeError: 'numpy.ndarray' object has no attribute 'autoscale_None'</code>.</p>
  <p>I am currently lacking orientation on how to proceed on this.
  Edit: I was marked as possible duplicate of <a href="https://stackoverflow.com/questions/6063876/matplotlib-colorbar-for-scatter">matplotlib colorbar for scatter</a>. I found that question already, but it didn't help with my problem. In my case, I need a colormap that is independent of a z-value, but will only indicate the &quot;row number&quot; or &quot;scatter-row&quot; or however you want to call it (equivalent to &quot;lines&quot; in a <code>plt.plot</code>).</p>