Legend colours are different from the plot

조회 수: 1 (최근 30일)
UserCJ
UserCJ 2022년 9월 29일
댓글: Star Strider 2022년 9월 29일
I'm trying to create a plot for different ϕ values with respect to and x for a function(f) when . I'm also creating the legend inside the loop. The code is working, but the legend colours are not matching with the plot.
clear all;
clc;
[x,xi] = meshgrid(0:0.001:1,0:0.002:1);
b = 3;
a = 1;
nphi = 7;
phi= [0 0.4 0.8 1 1.4 1.8 2];
for p = 1:nphi
f2 = @(x,a,b) a*b*(x)./(b-(1-x));
f1 = @(x,a,b) a*b*(1-x)./(b-x);
r = @(x,xi,a,b,phi) (1-(phi./2)).*xi.*f1(x,a,b) - (phi./2).*(1-xi).*f2(x,a,b);
R = r(x,xi,a,b, phi(p));
M_phi=contour(x,xi,R,[0 0]);
figure(2)
plot(M_phi(1,2:end),M_phi(2,2:end),'Linewidth',2)
legendInfo{p} = ['$\phi= $' num2str(phi(p))];
legend(legendInfo,'interpreter','latex',Location="southeast")
hold on
end
hold off
xlabel('$x$','interpreter','latex')
ylabel('$x_i$','interpreter','latex')
Here is the output of the function and there are multiple legend values with blue and there are only two plot with blue in the plot.
Can someone please help me to get this fixed? Thanks in advance!
  댓글 수: 2
Star Strider
Star Strider 2022년 9월 29일
@UserCJ — Please provide the ‘rs_a’ function. (I can’t run your code without it, and I can’t provide an appropriate reply without running it.)
UserCJ
UserCJ 2022년 9월 29일
@Star Strider Thanks for that! I edited the code. "rs_a" is not a function, it is just this;
r = @(x,xi,a,b,phi) (1-(phi./2)).*xi.*f1(x,a,b) - (phi./2).*(1-xi).*f2(x,a,b);

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

채택된 답변

Star Strider
Star Strider 2022년 9월 29일
Thank you for the ‘r’ anonymous function! I can now run your code.
The code was plotting the contours as well as the ϕ vectors. I changed the code to return only the line handles (the ‘hp{p}’ outputs of the plot calls), and added the 'DisplayName' to each plot call to avoid any ambiguity. I also ‘stepped through’ the loop with increasing values of the ‘nphi’ value to be certain that your code works as it should. It now appears to. (I also made some aesthetic tweaks.)
I’ll keep working on this if there is more to be done.
Try this —
% clear all;
% clc;
[x,xi] = meshgrid(0:0.001:1,0:0.002:1);
b = 3;
a = 1;
nphi = 7;
phi= [0 0.4 0.8 1 1.4 1.8 2];
for p = 1:nphi
f2 = @(x,a,b) a*b*(x)./(b-(1-x));
f1 = @(x,a,b) a*b*(1-x)./(b-x);
r = @(x,xi,a,b,phi) (1-(phi./2)).*xi.*f1(x,a,b) - (phi./2).*(1-xi).*f2(x,a,b);
R = r(x,xi,a,b, phi(p));
M_phi=contour(x,xi,R,[0 0]);
figure(2)
hp{p} = plot(M_phi(1,2:end),M_phi(2,2:end),'Linewidth',2, 'DisplayName',sprintf('$\\phi = %4.1f$',phi(p)));
% legendInfo{p} = ['$\phi= $' num2str(phi(p))];
% legend(legendInfo,'interpreter','latex',Location="southeast")
legend([hp{:}],'interpreter','latex',Location="southeast")
hold on
end
hold off
xlabel('$x$','interpreter','latex')
ylabel('$x_i$','interpreter','latex')
.
  댓글 수: 2
UserCJ
UserCJ 2022년 9월 29일
@Star Strider Thank you very much for your help! That worked. :)
Star Strider
Star Strider 2022년 9월 29일
As always, my pleasure!

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

추가 답변 (1개)

vamshi sai yele
vamshi sai yele 2022년 9월 29일
편집: vamshi sai yele 2022년 9월 29일
I have tried to run the code from my end but due to insufficient initializations I couldn't run to check the issue on first hand. Solution would have been more precise if code ran from my end
However, I have couple of suggestions to set this up.
Scenario-1
Assigning the name/title to the plot and then declaring the 'legend' will display in the necessary order. Below is snipet for the same.
Scenario-2
Mentioning the titles directly in the 'legend' function directly in the intended order as shown.
For your reference kindly check this link for more information.
  댓글 수: 1
UserCJ
UserCJ 2022년 9월 29일
In my situation, the legend is automatically created inside the loop. As you can see, all the values appear to be correct, but the colours are not correct with the plot colour. I know how to add manually if it's known, but this is looping over somne values and the legend is created inside it.

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

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by