Transparent shapes are not transparent to each other

4 ビュー (過去 30 日間)
Amir Raz
Amir Raz 2023 年 9 月 29 日
コメント済み: Voss 2023 年 9 月 29 日
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.

採用された回答

Voss
Voss 2023 年 9 月 29 日
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
  2 件のコメント
Amir Raz
Amir Raz 2023 年 9 月 29 日
Thanks!
Voss
Voss 2023 年 9 月 29 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by