Legend displaying the same color for all lines

60 visualizaciones (últimos 30 días)
Dragos Anghel
Dragos Anghel el 24 de Dic. de 2021
Comentada: Image Analyst el 24 de Dic. de 2021
INPUT(theta and R(theta,x) are just some 1D arrays):
figure(1)
%plot(theta,R(theta,1.1),theta,R(theta,2),theta,R(theta,3),'LineWidth',2)
plot(theta,R(theta,1.1),'g','LineWidth',2)%,'DisplayName','a=1.1');
hold on
plot(theta,R(theta,2),'r','LineWidth',2)%,'DisplayName','a=2');
plot(theta,R(theta,3),'b','LineWidth',2)%,'DisplayName','a=3');
legend('1.1','2','3')
hold off
legend.FontSize = 20;
ax = gca;
ax.FontSize = 25;
grid on
xticks(0:(2*pi/4):(2*pi))
xticklabels({'0' '\pi/2' '\pi' '3\pi/2' '2\pi'})
xlabel('\theta','FontSize',25)
ylabel('R','FontSize',25)
OUTPUT:
Can anyone explain what am I missing? I have been trying to get this legend to work for hours
  2 comentarios
Image Analyst
Image Analyst el 24 de Dic. de 2021
You're calling this function R(theta,1.1) but you forgot to give us the R() function and the theta variable.
Dragos Anghel
Dragos Anghel el 24 de Dic. de 2021
Here they are. Theta is just a vector of numbers from 0 to 2 pi in steps of 2 pi/1000. R() is some function that returns a vector with 1000 entries:
angle_steps = 1000;
theta = zeros(angle_steps);
for i=1:angle_steps
theta(i) = i*2*pi/angle_steps;
end
function R_factor = R(theta,a)
R_factor = -1/2*log((cos(theta)+a).*abs((3*cos(theta).^2+1)*a+2*cos(theta)*(a^2+1)));
end

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 24 de Dic. de 2021
You did the zeros(1000) wrong and so you made a matrix instead of a vector. Try this:
angle_steps = 1000;
theta = zeros(angle_steps, 1);
for i=1:angle_steps
theta(i) = i*2*pi/angle_steps;
end
figure(1)
%plot(theta,R(theta,1.1),theta,R(theta,2),theta,R(theta,3),'LineWidth',2)
plot(theta,R(theta,1.1),'g','LineWidth',2)%,'DisplayName','a=1.1');
hold on
plot(theta,R(theta,2),'r','LineWidth',2)%,'DisplayName','a=2');
plot(theta,R(theta,3),'b','LineWidth',2)%,'DisplayName','a=3');
legend('1.1','2','3')
hold off
legend.FontSize = 20;
ax = gca;
ax.FontSize = 25;
grid on
xticks(0:(2*pi/4):(2*pi))
xticklabels({'0' '\pi/2' '\pi' '3\pi/2' '2\pi'})
xlabel('\theta','FontSize',25)
ylabel('R','FontSize',25)
function R_factor = R(theta,a)
R_factor = -1/2*log((cos(theta)+a).*abs((3*cos(theta).^2+1)*a+2*cos(theta)*(a^2+1)));
end
  2 comentarios
Dragos Anghel
Dragos Anghel el 24 de Dic. de 2021
Indeed, this solved the problem. Thank you very much.
Image Analyst
Image Analyst el 24 de Dic. de 2021
@Dragos Anghel since you say my Answer solved the problem, can you click the "Accept this answer" link. Thanks in advance. 🙂

Iniciar sesión para comentar.


the cyclist
the cyclist el 24 de Dic. de 2021
This pair of lines causes a problem in my MATLAB setup (R2021b on an M1 Mac). I'm not sure if it valid MATLAB syntax in general.
legend('1.1','2','3')
legend.FontSize = 20;
Instead, try
L = legend('1.1','2','3')
L.FontSize = 20;

Star Strider
Star Strider el 24 de Dic. de 2021
Each plot call creates 1000 Line arrays.
Just pass the first of each to legend and it works —
angle_steps = 1000;
theta = zeros(angle_steps);
for i=1:angle_steps
theta(i) = i*2*pi/angle_steps;
end
figure(1)
%plot(theta,R(theta,1.1),theta,R(theta,2),theta,R(theta,3),'LineWidth',2)
hp1 = plot(theta,R(theta,1.1),'g','LineWidth',2);%,'DisplayName','a=1.1');
hold on
hp2 = plot(theta,R(theta,2),'r','LineWidth',2);%,'DisplayName','a=2');
hp3 = plot(theta,R(theta,3),'b','LineWidth',2);%,'DisplayName','a=3');
hl = legend([hp1(1);hp2(1);hp3(1)], '1.1','2','3');
hold off
hl.FontSize = 20;
ax = gca;
ax.FontSize = 25;
grid on
xticks(0:(2*pi/4):(2*pi))
xticklabels({'0' '\pi/2' '\pi' '3\pi/2' '2\pi'})
xlabel('\theta','FontSize',25)
ylabel('R','FontSize',25)
function R_factor = R(theta,a)
R_factor = -1/2*log((cos(theta)+a).*abs((3*cos(theta).^2+1)*a+2*cos(theta)*(a^2+1)));
end
.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by