How can I put legend for some of my curves?

1 回表示 (過去 30 日間)
Fatemeh Alaee
Fatemeh Alaee 2020 年 7 月 16 日
コメント済み: Fatemeh Alaee 2020 年 7 月 17 日
Hi, I've plotted two functions in one plot, in which each function is plotted based on different amount of "p" , i.e. there are 6 numbers for "p", for each function, and I have 12 curves in a plot. The point is my functions are "y1=f(x)" and "y2=-f(x)" , and every pair of functions ( y1, y2) has the same p. Is there a way that I can introduce each pair of curves with one "p" in legend? And those pair which has the same p, plot with the same color? Thank you in advance

採用された回答

Takumi
Takumi 2020 年 7 月 16 日
I created a example code.
col = [0 ,0 ,0;
255,0, 0 ;
0, 255,0 ;
0, 0, 255;
255,255,0 ;
0, 255,255]./255; % color array
func = @(x,p) p*sqrt(x); % some function
x = linspace(0,10);
p = 1:6; % some factor
y = zeros(length(p),length(x));
for i=1:length(p)
y(i,:) = func(x,p(i));
plot(x,y(i,:),'Color',col(i,:)); % +function
hold on
plot(x,-y(i,:),'Color',col(i,:),'HandleVisibility','off'); % -function, legend is not shown
end
legend(strcat('p=',num2str(p')))
  4 件のコメント
Takumi
Takumi 2020 年 7 月 17 日
編集済み: Takumi 2020 年 7 月 17 日
How about this way?
col = [0 ,0 ,0;
255,0, 0 ;
0, 255,0 ;
0, 0, 255;
255,255,0 ;
0, 255,255]./255; % color array
func = @(x,p) p*sqrt(x); % some function
x = linspace(0,10);
p = 1:6; % some factor
y = zeros(length(p),length(x));
for i=1:length(p)
y(i,:) = func(x,p(i));
s(i) = plot(x,y(i,:),'Color',col(i,:),'DisplayName',sprintf('p=%2.1f',p(i))); % +function
hold on
plot(x,-y(i,:),'Color',col(i,:));
end
legend(s)
Fatemeh Alaee
Fatemeh Alaee 2020 年 7 月 17 日
Thank you so much, Now it works fine

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by