How can I set the transparency of LINE objects in MATLAB 7.14 (R2012a)?
조회 수: 781 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2012년 6월 25일
댓글: Walter Roberson
2025년 7월 15일
I would like to set the transparency of LINE objects in my figures. I use the ALPHA command to set the transparency of objects in my figure. However, LINE objects do not respect ALPHA values even though I am using the OPENGL renderer.
채택된 답변
MathWorks Support Team
2012년 6월 25일
Line series objects do not support transparency (alpha) values. The following workaround can be used to overcome this limitation:
1. Please refer to the following MATLAB Central link:
2. The above function can be used to introduce transparency in LINE objects as follows:
EXAMPLE 1:
n = 10;
xs = rand(n,1);
ys = rand(n,1);
zs = rand(n,1)*3;
plot3(xs,ys,zs,'r.')
xlabel('x');ylabel('y');zlabel('z');
p = patchline(xs,ys,zs,'linestyle','--','edgecolor','g','linewidth',3,'edgealpha',0.2);
EXAMPLE 2:
t = 0:pi/64:4*pi;
p(1) = patchline(t,sin(t),'edgecolor','b','linewidth',2,'edgealpha',0.5);
p(2) = patchline(t,cos(t),'edgecolor','r','linewidth',2,'edgealpha',0.5);
l = legend('sine(t)','cosine(t)');
tmp = sort(findobj(l,'type','patch'));
for ii = 1:numel(tmp)
set(tmp(ii),'facecolor',get(p(ii),'edgecolor'),'facealpha',get(p(ii),'edgealpha'),'edgecolor','none')
end
댓글 수: 2
Walter Roberson
2019년 11월 6일
Karl Oskar Pires Bjørgen comments,
This is not the simplest approach, check fabian's answer below
Walter Roberson
2019년 11월 6일
The question is specifically about R2012a, at which time the functionality described by Fabian did not exist.
추가 답변 (1개)
Fabian
2015년 7월 31일
you could try setting a four-element vector for the color property of the line. works for me in 2014 b.
lh.Color=[0,0,0,0.5]
with lh handle to the line, producing a black line with transparency 0.5
댓글 수: 7
Star Strider
2022년 8월 3일
The 4-element colour vector seems to work in R2022a —
x = 0:0.01:2*pi;
y = sin(x);
figure
plot(x, y, '-', 'Color',[0.2 0.5 0.9 0.8], 'LineWidth',2.5) % Alpha = 0.8
hold on
plot(x, y+0.25, '-', 'Color',[0.2 0.5 0.9 0.2], 'LineWidth',2.5) % Alpha = 0.2
hold off
.
Walter Roberson
2025년 7월 15일
The 4-element colour vector continues to work in R2025a.
However, anything that causes the line object to be copied or regenerated with lose the alpha information, including:
- copyobj
- saving and loading the plot or portions of the plot
- sending the plot between parallel workers and the client
- resizing the axes
- print() or exportobj() (both of which cause the figure to be copied to a temporary figure, then internal properties are adjusted to fit the page size and resolution properties in effect, then the modified version is rendered.)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!