フィルターのクリア

Legend for fill_between plot

6 ビュー (過去 30 日間)
Vale Vale
Vale Vale 2023 年 3 月 29 日
コメント済み: Star Strider 2023 年 3 月 29 日
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.

回答 (1 件)

Antoni Garcia-Herreros
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
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 ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by