How to use alphadata for markers in scatter plot so each marker has a different alpha value?

73 views (last 30 days)
In the newest release of MATLAB marker transparency was implemented for scatter plots. However, as far as I can tell, one can only set a global alpha value that applies to ALL of the markers. I would like to have different alpha values for each marker, just like how one can assign different colors to each marker. How might I achieve this?
I envision something like:
SizeData = 10;
hS = scatter(X,Y,SizeData,ColorData,'filled',AlphaData);
I would like the result to be a scatter plot with the i-th marker located at [X(i),Y(i)] and colored by the value in ColorData(i), and having a transparency set by AlphaData(i). Any suggestions?
I have tried to achieve this using patch objects, since you can specify color and alpha data for each vertex, and try to just set the faces and edges to have no color. Unfortunately the FaceVertexCData and FaceVertexAlphaData don't seem to apply to the vertex Markers, all I can do is have it influence interpolated face color and face alpha, which is not what I'm going for.
yeungor on 6 Nov 2017
This solution does not have the intersection behavior of alpha, but a solution that worked for me was to use HSV coloring as a fade out instead of transparency.
I was showing a trail of datapoints in time and wished them to fade out after a certain amount of elements - I used an hsv colormap where datapoints fading out had a saturation near 0 and recently added datapoints had a value of 1.
numtrail = 10;
t = linspace(0,2*pi);
xdata = cos(t);
ydata = sin(t);
hsvcmap = [0.6*ones(numtrail,1), linspace(1,0,numtrail)', ones(numtrail,1)];
ax = scatter(xdata(1:numtrail), ydata(1:numtrail), [], hsv2rgb(hsvcmap), 'filled');
axis([-1 1 -1 1])
for frame = (numtrail+1):length(xdata)
ax.XData = [xdata(frame) ax.XData(1:end-1)];
ax.YData = [ydata(frame) ax.YData(1:end-1)];
However, I understand this might not work in your application.

Sign in to comment.

Answers (1)

Prannay Jain
Prannay Jain on 7 Apr 2017
I work for MathWorks and I have provided this feedback to the developers.
As a work around, you can set the alpha for every plot of the marker iteratively:
ax = axes;
hold on;
for i = 1:10
s = scatter(10*rand(1),10*rand(1),36,rand(1,3),'filled');
s.MarkerFaceAlpha = rand(1); %%marking every maker with different alpha value
Walter Roberson
Walter Roberson on 6 Nov 2017
The original question had AlphaData(i) -- alpha values the user had somehow computed. If there is a moderate possibility of different locations having the same alpha value, then then it is more efficient to combine those into a single scatter call. The answer from Prannay Jain calls for using a separate scatter() for every point -- at which point one would wonder whether it would be more efficient to line() each point into existence separately than to scatter() each point into existence separately.

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by