Semi-transparent markers in Matlab Figures
Asked Answered
D

6

10

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?

Deedee answered 16/6, 2011 at 2:9 Comment(2)
possible duplicate of Plot circles with alpha values in matlabBulwark
Maybe there wasn't a straightforward way to do that by the time this question was asked but now there's a way. Not sure since which version though. See this link..Concurrence
M
5

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.

Mozell answered 16/6, 2011 at 2:9 Comment(2)
Well, that's what I guessed and feared. Still, I'll leave the question open for a while just to make sure no one with an answer is hanging around. Thanks!Deedee
I concluded that there was no answer (well, no way to set the alpha values of plot markers) and still use my ag_plot_little_circles function.Martica
H
10

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
Hungerford answered 8/3, 2013 at 18:49 Comment(1)
Really a nice suggestion. I would like to add that, if one is interested in scaling the patch-size then simply apply multiplication to the sin() or cos() calls, e.g. sin(t)/3 would make the patches smaller.Summerly
M
5

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.

Mozell answered 16/6, 2011 at 2:9 Comment(2)
Well, that's what I guessed and feared. Still, I'll leave the question open for a while just to make sure no one with an answer is hanging around. Thanks!Deedee
I concluded that there was no answer (well, no way to set the alpha values of plot markers) and still use my ag_plot_little_circles function.Martica
L
4

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

Luther answered 20/4, 2015 at 14:53 Comment(2)
Only for Matlab 2014 and after.Fishplate
Take note of the warnings about using legend and the workaround provided at that same blog post.Humbuggery
I
3

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;

Improper answered 1/12, 2016 at 14:13 Comment(1)
Interestingly, with Matlab 2019 the plot handle still doesn't have this property. For scatter though you also have MarkerEdgeAlpha.Pegeen
R
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
Rush answered 5/1, 2014 at 15:20 Comment(0)
R
0

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
Risa answered 22/6, 2016 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.