2

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.
Argentina
  • 1,071
  • 5
  • 16
  • 30

3 Answers3

8

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)
Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75
2

Another option is to use a the cmap argument to provide a colormap, and the c argument to provide mappings of how dark/light you want the colors. Check out this question: matplotlib colorbar for scatter

Here's all the matplotlib colormaps: http://matplotlib.org/examples/color/colormaps_reference.html I suggest a sequential colormap like PuRd. If the colors are getting darker in the opposite direction, you can use the "reversed" colormap by appending _r to the name, like PuRd_r.

Try this out:

ax1.scatter(p_100, tgw_e100, c=ecc_100, cmap='PuRd', s=10, label='time 0')

Hope that helps!

Community
  • 1
  • 1
Olga Botvinnik
  • 1,564
  • 1
  • 14
  • 32
0

Here is a scatter plot of three columns using transparency.

x = sample_df['feature_1']
y = sample_df['feature_2']
#e = {'label_x': 'b', 'label_y': 'r'}
# label_x will be in red, label_y will be in blue
e = {'label_x': np.asarray((1, 0, 0, .1)), 'label_y': np.asarray((0, 0, 1, .1))} 
colr = sample_df['label_bc'].map(e)
plt.scatter(x, y, c=colr);
netskink
  • 4,033
  • 2
  • 34
  • 46