Legend for fill_between plot
조회 수: 6 (최근 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!