plot using user custumized legend lines
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
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
카테고리
도움말 센터 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

