Legend displaying the same color for all lines
65 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Image Analyst
il 24 Dic 2021
You're calling this function R(theta,1.1) but you forgot to give us the R() function and the theta variable.
Risposte (3)
Image Analyst
il 24 Dic 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 Commenti
Image Analyst
il 24 Dic 2021
@Dragos Anghel since you say my Answer solved the problem, can you click the "Accept this answer" link. Thanks in advance. 🙂
the cyclist
il 24 Dic 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;
0 Commenti
Star Strider
il 24 Dic 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
.
0 Commenti
Vedere anche
Categorie
Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!