Transparent shapes are not transparent to each other

2 views (last 30 days)
I am trying to create a plot of a transparent cylinder with transparent shapes overlayed on top of it. The shapes are transparent to each other when the cylinder is not present, but only transparent to the cylinder when it is present. I'm not sure how to fix this. My code is:
fg = figure(1); clf;
set(fg,'Units','centimeters')
pos = [14,5,16,8];
set(fg,'Position',pos)
[X,Y,Z] = cylinder(0.11);
S = surf(Z,X,Y,'FaceColor','k','FaceAlpha',0.2,'EdgeColor','none');
rotate(S,[0,1,0],20)
X_n = get(S,"XData");
Y_n = get(S,"YData");
Z_n = get(S,"ZData")-1;
clf;
S_n = surf(X_n,Y_n,Z_n,'FaceColor','k','FaceAlpha',0.15,'EdgeColor','none');
view(2)
ylim([-0.25,0.25])
xlim([-0.0,1.0])
grid off
set(gca,'XTick',{},'YTick',{},'XColor','w','YColor','w')
hold on
f = @(x,a) 0.2*exp(-300*(x-a).^2.);
x = 0:0.001:1;
for k = 0:10
if mod(k,3)
plot(x,f(x,k*0.1)-0.1,'r')
patch([0,x,1],([0,f(x,0.1*k),0]-0.1),'r',"FaceAlpha",0.2,"EdgeColor",'r',"EdgeAlpha",0.1)
if k ~= 10
plot(k*0.1,0,'r+')
end
else
plot(x,f(x,k*0.1)-0.1,'b')
patch([0,x,1],([0,f(x,0.1*k),0]-0.1),'b', 'FaceAlpha', 0.2,"EdgeColor",'b',"EdgeAlpha",0.1)
if k
plot(k*0.1,0,'bx')
end
end
end
hold off
I've tried changing patch to a 3-d patch, and using fill, fill3 and polyshape with plot, but they all produce the same results. Commenting out the cyinder surface plot makes the shapes transparent to eachother again, but for some reason including this surface changes this.

Accepted Answer

Voss
Voss on 29 Sep 2023
Using the 'painters' Renderer fixes the problem:
fg = figure(1); clf;
fg.Renderer = 'painters';
set(fg,'Units','centimeters')
pos = [14,5,16,8];
set(fg,'Position',pos)
[X,Y,Z] = cylinder(0.11);
S = surf(Z,X,Y,'FaceColor','k','FaceAlpha',0.2,'EdgeColor','none');
rotate(S,[0,1,0],20)
X_n = get(S,"XData");
Y_n = get(S,"YData");
Z_n = get(S,"ZData")-1;
% clf;
delete(S);
S_n = surf(X_n,Y_n,Z_n,'FaceColor','k','FaceAlpha',0.15,'EdgeColor','none');
view(2)
ylim([-0.25,0.25])
xlim([-0.0,1.0])
grid off
set(gca,'XTick',{},'YTick',{},'XColor','w','YColor','w')
hold on
f = @(x,a) 0.2*exp(-300*(x-a).^2.);
x = 0:0.001:1;
for k = 0:10
if mod(k,3)
plot(x,f(x,k*0.1)-0.1,'r')
patch([0,x,1],([0,f(x,0.1*k),0]-0.1),'r',"FaceAlpha",0.2,"EdgeColor",'r',"EdgeAlpha",0.1)
if k ~= 10
plot(k*0.1,0,'r+')
end
else
plot(x,f(x,k*0.1)-0.1,'b')
patch([0,x,1],([0,f(x,0.1*k),0]-0.1),'b', 'FaceAlpha', 0.2,"EdgeColor",'b',"EdgeAlpha",0.1)
if k
plot(k*0.1,0,'bx')
end
end
end
hold off

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!