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?

Ruggiero Spearman
  • 6,735
  • 5
  • 26
  • 37
  • 1
    possible duplicate of [Plot circles with alpha values in matlab](http://stackoverflow.com/questions/5893513/plot-circles-with-alpha-values-in-matlab) – Jonas Jun 16 '11 at 03:25
  • 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.](https://www.mathworks.com/examples/matlab/mw/graphics-ex36949892-create-scatter-chart-with-transparency). – akilat90 Mar 17 '17 at 18:48

6 Answers6

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
user2149589
  • 101
  • 1
  • 3
  • 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. – Shadow Oct 21 '13 at 11:48
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.

Community
  • 1
  • 1
abcd
  • 41,765
  • 7
  • 81
  • 98
  • 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! – Ruggiero Spearman Jun 16 '11 at 03:48
  • 3
    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. – Alex Jun 16 '11 at 06:31
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

Zach
  • 41
  • 2
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;

Matt
  • 31
  • 1
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
Community
  • 1
  • 1
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