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 Center および File Exchange2-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