title: Scatter plot using colormap and individual alpha values
body: <p>There are two similar questions about this:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/24767355/individual-alpha-values-in-scatter-plot">Individual alpha values in scatter plot</a></li>
  <li><a href="https://stackoverflow.com/questions/37327308/add-alpha-to-an-existing-matplotlib-colormap">Add alpha to an existing matplotlib colormap</a></li>
  </ol>
  
  <p>but neither of these address this issue. I need to produce a scatter plot with individual alphas (as in question 1.) but I need to combine this with a  given colormap (as in question 2.)</p>
  
  <p>This is what I came up with:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pylab as plt
  from matplotlib.colors import ListedColormap
  
  x = np.arange(10)
  y = np.arange(10)
  # These are the colors for my data
  z = np.arange(10)
  # These are the alpha values for my data
  alphas = np.linspace(0.1, 1, 10)
  
  # Color map I want to use
  cm = plt.cm.get_cmap('viridis')
  # Get the colormap colors for my data
  my_cmap = cm(z)
  # Set alpha
  my_cmap[:, -1] = alphas
  # Create new colormap
  my_cmap = ListedColormap(my_cmap)
  
  plt.subplot(121)
  plt.scatter(x, y, cmap=cm, c=z)
  plt.subplot(122)
  plt.scatter(x, y, cmap=my_cmap, c=z)
  plt.show()
  </code></pre>
  
  <p>but the result is not what I expect:</p>
  
  <p><a href="https://i.stack.imgur.com/4Yoyw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Yoyw.png" alt="enter image description here"></a></p>
  
  <p>where the image to the left is what the scatter plot looks like using the colormap and no alphas, and the plot to the right is my attempt to add individual alphas to the data points.</p>