plot using user custumized legend lines

3 views (last 30 days)
Hamid
Hamid on 28 Jun 2022
Commented: Voss on 29 Jun 2022
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

Accepted Answer

Voss
Voss on 28 Jun 2022
Edited: Voss on 28 Jun 2022
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

More Answers (1)

KSSV
KSSV on 28 Jun 2022
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

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!