Legend for fill_between plot
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Can you please help me to create the correct code for this legend? i need to insert a legend for every filled area.
x = [1 2 3 4 5];
a = [5 9 3 2 1];
b = [6 11 2 4 5];
c = [11 2 4 1 6];
d = [6 1 3 0 5];
opts1={'EdgeColor', 'none',...
'FaceColor', [1 0.5 0.5]};
opts2={'EdgeColor', 'none',...
'FaceColor', [1 1 0]};
p1=fill_between(x,a,b,[], opts1{:},'facealpha',0.2);
hold on
p2=fill_between(x,c,d,[], opts2{:},'facealpha',0.2);
axis tight
hold off
legend([p1 p2],'Area 1','Area 2')
At the moment I managed to have only lines in the legend. I need the regtlangle with the color of the area instead.

0 commentaires
Réponses (1)
Antoni Garcia-Herreros
le 29 Mar 2023
Hello,
Not sure how your fill_between function works, but you can use the patch build in function, refer to this thread or this one.
x = [1 2 3 4 5];
a = [5 9 3 2 1];
b = [6 11 2 4 5];
c = [11 2 4 1 6];
d = [6 1 3 0 5];
opts1={'EdgeColor', 'none',...
'FaceColor', [1 0.5 0.5]};
opts2={'EdgeColor', 'none',...
'FaceColor', [1 1 0]};
p1=patch([x fliplr(x)], [a fliplr(b)], 'b','facealpha',0.2);
hold on
p2=patch([x fliplr(x)], [c fliplr(d)], 'r','facealpha',0.2);
axis tight
legend([p1 p2],'Area 1','Area 2')
1 commentaire
Star Strider
le 29 Mar 2023
@Antoni Garcia-Herreros — First, I appreciate your referencing my code!
Second, you can run posted code by using the green right-arrow RUN button in the top toolstrip, here giving:
x = [1 2 3 4 5];
a = [5 9 3 2 1];
b = [6 11 2 4 5];
c = [11 2 4 1 6];
d = [6 1 3 0 5];
opts1={'EdgeColor', 'none',...
'FaceColor', [1 0.5 0.5]};
opts2={'EdgeColor', 'none',...
'FaceColor', [1 1 0]};
p1=patch([x fliplr(x)], [a fliplr(b)], 'b','facealpha',0.2);
hold on
p2=patch([x fliplr(x)], [c fliplr(d)], 'r','facealpha',0.2);
axis tight
legend([p1 p2],'Area 1','Area 2')
.
Voir également
Catégories
En savoir plus sur Axis Labels 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!
