plot using user custumized legend lines

4 visualizaciones (últimos 30 días)
Hamid
Hamid el 28 de Jun. de 2022
Comentada: Voss el 29 de Jun. de 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

Respuesta aceptada

Voss
Voss el 28 de Jun. de 2022
Editada: Voss el 28 de Jun. de 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
  2 comentarios
Hamid
Hamid el 29 de Jun. de 2022
Thanks a lot! That was very helpful.
Voss
Voss el 29 de Jun. de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 28 de Jun. de 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

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by