How to use alphadata for markers in scatter plot so each marker has a different alpha value?
53 次查看(过去 30 天)
显示 更早的评论
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.
3 个评论
yeungor
2017-11-6
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)];
figure(1),clf
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)];
drawnow
end
However, I understand this might not work in your application.
回答(1 个)
Prannay Jain
2017-4-7
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
end
3 个评论
yeungor
2017-11-6
@Walter Roberson Do you mind if I ask what you mean? Because to me, it looks like that is what they are doing. (I'm assuming random won't repeat values within 10 calls, which isn't correct, but it seems unlikely)
Walter Roberson
2017-11-6
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!