Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting scatter points transparency from data array in matplotlib plot

I am plotting data with matplotlib, I have obtained a scatter plot from two numpy arrays:

ax1.scatter(p_100,tgw_e100,color='m',s=10,label="time 0")

I would like to add information about the eccentricity of each point. For this purpose I have a third array of the same length of p_100 and tgw_e100, ecc_100 whose items range from 0 to 1.

So I would like to set the transparency of my points using data from ecc_100 creating some sort of shade scale.

I have tried this:

ax1.scatter(p_100,tgw_e100,color='m',alpha = ecc_100,s=10,label="time 0")

But I got this error:

ValueError: setting an array element with a sequence.
like image 932
Argentina Avatar asked Mar 29 '26 21:03

Argentina


1 Answers

According to the documentation alpha can only be a scalar value.

Thus I can't see any other way than looping over all your point one by one.

for x, y, a in zip(p_100, tgw_e100, ecc_100):
    ax1.scatter(x, y, color='m',alpha = a, s=10)

I think the labelling will be quite weird though, so you might have to create the legend by hand. I omitted that from my solution.

I guess a patch to make the alpha keyword argument behave like c and s would be welcome.

Update May 6 2015 According to this issue, changing alpha to accept an array is not going to happen. The bug report suggests to set the colors via an RGBA array to control the alpha value. Which sounds better than my suggestion to plot each point by itself.

c = np.asarray([(0, 0, 1, a) for a in alpha])
scatter(x, y, color=c, edgecolors=c)
like image 119
Hannes Ovrén Avatar answered Apr 01 '26 09:04

Hannes Ovrén