Search code examples
pythonmatplotlibalphaalpha-transparency

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.

Solution

  • 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!