Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 429
Ruggiero Spearman Avatar asked Jun 16 '11 02:06

Ruggiero Spearman


People also ask

How do you make a figure transparent in Matlab?

In the figure window, select property inspector by double clicking on your plot. There is a box styling section in which you can select the background color, by selecting none you can have it transparent.

How do you add a marker to a figure in Matlab?

Add markers in one of these ways: Include a marker symbol in the line-specification input argument, such as plot(x,y,'-s') . Specify the Marker property as a name-value pair, such as plot(x,y,'Marker','s') .

How do you make a patch transparent in Matlab?

Transparency for Individual Patches For constant transparency across the entire patch, set the FaceVertexAlphaData to a constant between 0 (fully transparent) and 1 (fully opaque), and set the FaceAlpha property to 'flat' .


2 Answers

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
like image 56
user2149589 Avatar answered Oct 13 '22 16:10

user2149589


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.

like image 41
2 revs Avatar answered Oct 13 '22 16:10

2 revs