scatter plot individual alpha values

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.

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.

Would it be possible to do something like

ax.plot(x, y, color=[c1, c2, c3], alpha=[a1, a2, a3])?

It doesn't seem that it's supported and that would be the 'natural'
extension, in some sense.

···

On Wed, Aug 08, 2012 at 05:22:49PM -0400, Tony Yu wrote:

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?
>
You can use a colormap with varying alpha values.

--
Damon McDougall
http://damon-is-a-geek.com
B2.39
Mathematics Institute
University of Warwick
Coventry
West Midlands
CV4 7AL
United Kingdom

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.

The ‘color’ argument of the ‘scatter’ function can take an array of (r,g,b,alpha) values:

In [23]: x = array([0, 1, 2, 3])

In [24]: y = array([0, 1, 1, 2])

In [25]: c = array([[0,0,1,1], [0,0,1,0.5], [0,1,0,0.5], [1,0,1,.9]])

In [26]: scatter(x, y, color=c)

Out[26]: <matplotlib.collections.PathCollection at 0x898adf0>

Warren

···

On Wed, Aug 8, 2012 at 3:34 PM, Gustavo Goretkin <gustavo.goretkin@…287…> wrote:


Live Security Virtual Conference

Exclusive live event will cover all the ways today’s security and

threat landscape has changed and how IT managers can respond. Discussions

will include endpoint security, mobile security and the latest in malware

threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I would also love to have this implemented. That is being able to not only
set the colors, but also the alpha values as an array.

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/scatter-plot-individual-alpha-values-tp21106p39671.html
Sent from the matplotlib - users mailing list archive at Nabble.com.