Legend displaying the same color for all lines
조회 수: 65 (최근 30일)
이전 댓글 표시
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
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.
답변 (3개)
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
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
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;
댓글 수: 0
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
.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!