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

1 回表示 (過去 30 日間)
Sim
Sim 2023 年 6 月 6 日
コメント済み: Sim 2023 年 6 月 8 日
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 Center および File ExchangePolygons についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by