フィルターのクリア

plot using user custumized legend lines

2 ビュー (過去 30 日間)
Hamid
Hamid 2022 年 6 月 28 日
コメント済み: Voss 2022 年 6 月 29 日
Hello all!
I wanted to change the color of the legend lines in order to have the same colors of the plotted circles in the graph (here each circle is presenting the radius calculated using K variable). I tried different methods but not worked. Please find the graph and my code attached. Thank you in advance.
K = [100;200;400;600;800;1000;1200;1400;1600;1800;2000];
a = 1;
B=0.2;
r = radius_cal (K,B) ;
figure
yline(0)
hold on
for i=1 :1: length(r)
circle (0,0,r(i));
hold on
xline (r(i))
hold on
end
hold on
yline (a,'Color','b')
fontSize = 14;
xlabel('radius')
legend({'100','200','400','600','800','1000','1200','1400','1600','1800','2000'})
grid minor
function r = radius_cal (K,B)
E0= my_const.m_mu_kg .* my_const.C .* my_const.C .* 6241506479963.2 ;
gamma = 1+ (K ./ E0);
v = my_const.C * sqrt (1- (1./(gamma .* gamma)));
omega= ((my_const.q .* B) ./ my_const.m_mu_kg) .* sqrt (1- (v .* v) ./ (my_const.C .* my_const.C) );
r= v ./ omega;
end
function circle(x,y,r)
ang=0:0.001:2*pi / 4;
xp=r*cos(ang);
yp=r*sin(ang);
plot(x+xp,y+yp);
end

採用された回答

Voss
Voss 2022 年 6 月 28 日
編集済み: Voss 2022 年 6 月 28 日
The set of colors of the lines in the legend doesn't match with the set of colors of the circles because the xlines and ylines are also being used in the legend. To fix that, you can make the legend based on the circles only.
K = [100;200;400;600;800;1000;1200;1400;1600;1800;2000];
a = 1;
B=0.2;
r = radius_cal (K,B) ;
figure
hold on % (you can do "hold on" only once, at the top)
yline(0)
h = zeros(1,numel(r)); % h: array of circle lines
for i = 1:numel(r)
h(i) = circle (0,0,r(i),K(i));
xline (r(i))
end
yline (a,'Color','b')
fontSize = 14;
xlabel('radius')
legend(h) % legend based on the lines in h only
grid minor
function r = radius_cal (K,B)
E0= my_const.m_mu_kg .* my_const.C .* my_const.C .* 6241506479963.2 ;
gamma = 1+ (K ./ E0);
v = my_const.C * sqrt (1- (1./(gamma .* gamma)));
omega= ((my_const.q .* B) ./ my_const.m_mu_kg) .* sqrt (1- (v .* v) ./ (my_const.C .* my_const.C) );
r= v ./ omega;
end
function h = circle(x,y,r,K)
% pass in a K value to be used as the DisplayName
% of the line (for the legend)
ang=0:0.001:2*pi / 4;
xp=r*cos(ang);
yp=r*sin(ang);
% return the plotted line (h):
h = plot(x+xp,y+yp,'DisplayName',num2str(K));
end
  2 件のコメント
Hamid
Hamid 2022 年 6 月 29 日
Thanks a lot! That was very helpful.
Voss
Voss 2022 年 6 月 29 日
You're welcome!

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

その他の回答 (1 件)

KSSV
KSSV 2022 年 6 月 28 日
K = [100;200;400;600;800;1000;1200;1400;1600;1800;2000];
a = 1;
B=0.2;
r = radius_cal (K,B) ;
c = {'r','g','b','c','m','y','k'} ; % specify your colors here.. length of c should be equal to length of r
figure
yline(0)
hold on
for i=1 :1: length(r)
[xp,yp] = circle (0,0,r(i)); % <---- get the coordinates of the circle
plot(xp,yp,'color',c{i}) %<---- plot the circle with specified colors in cell array c
xline (r(i))
end
hold on
yline (a,'Color','b')
fontSize = 14;
xlabel('radius')
legend({'100','200','400','600','800','1000','1200','1400','1600','1800','2000'})
grid minor
function r = radius_cal (K,B)
E0= my_const.m_mu_kg .* my_const.C .* my_const.C .* 6241506479963.2 ;
gamma = 1+ (K ./ E0);
v = my_const.C * sqrt (1- (1./(gamma .* gamma)));
omega= ((my_const.q .* B) ./ my_const.m_mu_kg) .* sqrt (1- (v .* v) ./ (my_const.C .* my_const.C) );
r= v ./ omega;
end
% Function is changed so that, it will give the coordinates of circle.
function [xp,yp] = circle(x,y,r)
ang=0:0.001:2*pi / 4;
xp=r*cos(ang);
yp=r*sin(ang);
end
  1 件のコメント
Hamid
Hamid 2022 年 6 月 28 日
Thank you so much.

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

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by