Legend displaying the same color for all lines

조회 수: 65 (최근 30일)
Dragos Anghel
Dragos Anghel 2021년 12월 24일
댓글: Image Analyst 2021년 12월 24일
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
Image Analyst
Image Analyst 2021년 12월 24일
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 2021년 12월 24일
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

댓글을 달려면 로그인하십시오.

답변 (3개)

Image Analyst
Image Analyst 2021년 12월 24일
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
Dragos Anghel
Dragos Anghel 2021년 12월 24일
Indeed, this solved the problem. Thank you very much.
Image Analyst
Image Analyst 2021년 12월 24일
@Dragos Anghel since you say my Answer solved the problem, can you click the "Accept this answer" link. Thanks in advance. 🙂

댓글을 달려면 로그인하십시오.


the cyclist
the cyclist 2021년 12월 24일
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 2021년 12월 24일
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
.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by