필터 지우기
필터 지우기

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

카테고리

Help CenterFile Exchange에서 Grid 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