title: Get both colorbar and individual alphas in scatter plot
body: <p>This is not a duplicate of <a href="https://stackoverflow.com/questions/37327308/add-alpha-to-an-existing-matplotlib-colormap">Add alpha to an existing matplotlib colormap</a> because that question was vague, and the only answer incorporates the alpha into the colormap (meaning color and alpha are inexorably tied together).</p>
  
  <p>In my example, alpha and color are determined from two <strong>different</strong> channels of information.</p>
  
  <p><strong>Edit:</strong> The second proposed duplicate link has a valid solution, so no need to reopen.</p>
  
  <hr>
  
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  import matplotlib.pyplot as plt
  import matplotlib.cm as cm
  
  x = np.linspace(0, 10)
  y = np.cos(x)
  value = 10 * np.cos(x / 3)
  alpha = y**2
  
  fig, ax = plt.subplots(figsize=(4, 3))
  </code></pre>
  
  <p>I want to apply a colormap to <code>value</code>, and use individual alphas for each point.  I also want a colorbar.  Plotting a colorbar with no alpha is easy:</p>
  
  <pre class="lang-py prettyprint-override"><code>ax.set(title='plot with correct colorbar')
  scatter = ax.scatter(x, y, c=value, cmap='hsv')
  fig.colorbar(scatter)
  </code></pre>
  
  <p>Adding individual alphas in <code>scatter</code> is possible if you supply RGBA data for <code>c</code> (the <code>alpha</code> argument cannot be used because it requires a scalar), but then the colorbar does not have access to <code>values</code> and will just output a generic <code>[0, 1]</code> bar:</p>
  
  <pre class="lang-py prettyprint-override"><code>normalize_max = lambda arr: arr / arr.max()
  to_zero_one = lambda arr: normalize_max(arr - arr.min())
  
  cmap = cm.get_cmap('hsv')
  C = np.zeros((len(x), 4))
  C[:, 3] = alpha
  C[:, :3] = cmap(to_zero_one(value))[:, :3]
  
  ax.set(title='plot with correct alpha')
  scatter = ax.scatter(x, y, c=C)
  fig.colorbar(scatter)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/DN5da.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DN5da.png" alt="plot with correct colorbar"></a>
  <a href="https://i.stack.imgur.com/1vlVp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1vlVp.png" alt="plot with correct alpha"></a></p>
  
  <p>How can I have both the correct colorbar and alpha?</p>