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

2 vues (au cours des 30 derniers jours)
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);

Réponse acceptée

Simon Chan
Simon Chan le 6 Juin 2023
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);

Plus de réponses (0)

Catégories

En savoir plus sur Lighting, Transparency, and Shading dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by