开发者

Semi-transparent markers in Matlab Figures

I 开发者_如何转开发want to plot a scatter plot with filled markers and make them semi-transparent so when two or more markers overlap, the overlapping area will be more opaque.

I naively thought

sg = scatter(rand(1000,1),rand(1000,1), 'filled');
alpha(0.5)

would work, but it doesn't. Also

set(get(sg, 'Children'), 'FaceAlpha', 0.2)

doesn't work. Any ideas?


Here's some sample matlab code that makes transparent scatterplot points with patch objects:

x=randn(5000,1)*20;
y= randn(5000,1)*20;
t= 0:pi/10:2*pi;
figure();
for i=1:size(x)
    pb=patch((sin(t)+ x(i)),(cos(t)+y(i)),'b','edgecolor','none');
    alpha(pb,.1);
end


AFAIK, you cannot change the alpha values of the plot markers in scatter. One solution would be to patch to draw markers yourself. Alpha values can be set for patch() objects and you will get the desired effect when markers overlap. However, this can get quite cumbersome and will need to be customized to your needs.

See this related question, where the function defined in the question does exactly that. You can use that as a starting point and work from there.


You can actually go about this without using patch. The example below uses the hidden MarkerHandle to let you access transparency. All you have to provide is the rgb code for the color you want and the transparency level on the same scale. The example below plots the random markers in a transparent red with 10% opacity by setting FaceColorData to uint8(255*[1;0;0;0.1])

sg = scatter(rand(1000,1),rand(1000,1), 'filled');
sMarkers=sg.MarkerHandle; %hidden marker handle
sMarkers.FaceColorData = uint8(255*[1;0;0;0.1]); %fourth element allows setting alpha
sMarkers.EdgeColorData = uint8(255*[1;0;0;0]); %set edge color in a similar way

EDIT: It seems that MATLAB will change these properties without warning when you resize, save...or apparently just look at it funny.

Based on http://undocumentedmatlab.com/blog/plot-markers-transparency-and-color-gradient


I am not sure about the previous versions, but Matlab 2016 seems to have the feature you are looking for:

sg = scatter(rand(1000,1),rand(1000,1), 'filled');

sg.MarkerFaceAlpha = 0.1;


Here is a function I used to create a semi-transparent scatter.

* This is a modified version of user2149589 answer (a bit more matlab-friendly).

function scatterPoints = transparentScatter(x,y,sizeOfCirlce,opacity)
% usage example:
% scatterPoints = transparentScatter(randn(5000,1),randn(5000,1),0.1,0.05);
% set(scatterPoints,'FaceColor',[1,0,0]);


    defaultColors = get(0,'DefaultAxesColorOrder');
    assert(size(x,2)  == 1 && size(y,2)  == 1 , 'x and y should be column vectors');
    t= 0:pi/10:2*pi;

    rep_x = repmat(x',[size(t,2),1]);
    rep_y = repmat(y',[size(t,2),1]);
    rep_t = repmat(t',[ 1, size(x,1)]);

    scatterPoints = patch((sizeOfCirlce*sin(rep_t)+ rep_x),(sizeOfCirlce*cos(rep_t)+rep_y),defaultColors(1,:),'edgecolor','none');
    alpha(scatterPoints,opacity);

end


The code above is a nice little function (for those of us still pre-2014b), but can be improved with a call to 'DataAspectRatio' and an adjustment of the patch size to make sure that the circles look like circles:

function scatterPoints = transparentScatter(x,y,sizeOfCirlce,opacity)
% usage example:
% scatterPoints = transparentScatter(randn(5000,1),randn(5000,1),0.1,0.05);
% set(scatterPoints,'FaceColor',[1,0,0]);

    dRatio = get(gca,'DataAspectRatio');
    dRatio = dRatio(1) / dRatio(2);
    defaultColors = get(0,'DefaultAxesColorOrder');
    assert(size(x,2)  == 1 && size(y,2)  == 1 , 'x and y should be column vectors');
    t= 0:pi/10:2*pi;

    rep_x = repmat(x',[size(t,2),1]);
    rep_y = repmat(y',[size(t,2),1]);
    rep_t = repmat(t',[ 1, size(x,1)]);

    scatterPoints = patch((dRatio*sizeOfCirlce*sin(rep_t)+ rep_x),(sizeOfCirlce*cos(rep_t)+rep_y),defaultColors(1,:),'edgecolor','none');
    alpha(scatterPoints,opacity);

end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜