You can use a colormap with varying alpha values.
Example below. I’m not a huge fan of the call signature of LinearSegmentedColormap
, so I’ve included a class that makes it a little more convenient to define the colormap.
Cheers,
-Tony
P.S. There were some major cleanups of the alpha handling not too long ago, but I don’t think those are necessary for scatter. Let me know if this doesn’t work for some reason.
#~~~ example code
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
class LinearColormap(LinearSegmentedColormap):
def init(self, name, segmented_data, index=None, **kwargs):
if index is None:
If index not given, RGB colors are evenly-spaced in colormap.
index = np.linspace(0, 1, len(segmented_data[‘red’]))
for key, value in segmented_data.iteritems():
Combine color index with color values.
segmented_data[key] = zip(index, value)
segmented_data = dict((key, [(x, y, y) for x, y in value])
for key, value in segmented_data.iteritems())
LinearSegmentedColormap.init(self, name, segmented_data, **kwargs)
Red for all values, but alpha changes linearly from 0.3 to 1
color_spec = {‘blue’: [0.0, 0.0],
‘green’: [0.0, 0.0],
‘red’: [0.8, 0.8],
‘alpha’: [0.3, 1.0]}
alpha_red = LinearColormap(‘alpha_red’, color_spec)
x, y, z = np.random.normal(size=(3, 100))
plt.scatter(x, y, c=z, cmap=alpha_red, s=50, edgecolors=‘none’)
plt.show()
Here’s a slightly more complicated use of LinearColormap, if you’re interested.
Blue below midpoint of colormap, red above mid point.
Alpha maximum at the edges, minimum in the middle.
bwr_spec = {‘blue’: [0.4, 0.4, 0.1, 0.1],
‘green’: [0.2, 0.2, 0.0, 0.0],
‘red’: [0.02, 0.02, 0.4, 0.4],
‘alpha’: [1, 0.3, 0.3, 1]}
blue_white_red = LinearColormap(‘blue_white_red’, bwr_spec,
index=[0, 0.5, 0.5, 1])
···
On Wed, Aug 8, 2012 at 4:34 PM, Gustavo Goretkin <gustavo.goretkin@…287…> wrote:
I can use the scatter function to plot an array of points and give a
corresponding array of colors to set those points. Is it possible to
do the same thing with alpha values?
Right now, I’m restoring to calling plot with an ‘o’ marker for each
individual point, which is slow.