필터 지우기
필터 지우기

Transparent patches in the main figure, but no transparent patches in the legend

조회 수: 4 (최근 30일)
In the following example, how can I have transparent patches in the main figure and No transparent patches in the legend?
N = 100;
x = linspace(0,1,N);
y1 = repmat([-2;2],1,N);
y2 = repmat([-1;1],1,N);
figure; hold on;
h1 = fill([x flip(x)], [y1(1,:) flip(y1(2,:))],'r');
h2 = fill([x flip(x)], [y2(1,:) flip(y2(2,:))],'g');
ylim([-2,2]);
first = legend('area1','area2','NumColumns',2);
PatchInLegend = findobj(first.PlotChildren, 'type', 'patch');
% Here below, FaceAlpha is applied to both patches in the main figure
% and in the legend - But I would need the transparency only for the main figure's patches
set(PatchInLegend(1), 'FaceAlpha', 0.2);
set(PatchInLegend(2), 'FaceAlpha', 0.4);

채택된 답변

Simon Chan
Simon Chan 2023년 6월 6일
One possible way is to use dummy handles.
N = 100;
x = linspace(0,1,N);
y1 = repmat([-2;2],1,N);
y2 = repmat([-1;1],1,N);
figure; hold on;
h1 = fill([x flip(x)], [y1(1,:) flip(y1(2,:))],'r','FaceAlpha',0.2);
h2 = fill([x flip(x)], [y2(1,:) flip(y2(2,:))],'g','FaceAlpha',0.4);
h3 = fill(NaN,NaN,'r'); % Dummy one for red
h4 = fill(NaN,NaN,'g'); % Dummy one for green
ylim([-2,2]);
legend([h3 h4],{'area1','area2'},'NumColumns',2);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by