Legend for fill_between plot
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Antoni Garcia-Herreros
2023-3-29
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 个评论
Star Strider
2023-3-29
@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')
.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!