Problem with plotting - legend

Hi all, Any idea how to show the lines in the legend show in the same colour as I defined in my code, black, green, and red? for some reason they all look green ! Here is my code, I also attached the figure. Thank you
plot(T3_All(mask),Tmax_All(mask),'o',T3_All,Res_Lin,'k',T3_All,Res_Pwr,'g', T3_All,Res_Exp,'r'),...
xlabel('T3 (K)'),ylabel('Residuals (K)'),...
legend('Data','Linear','Power','Exponential','location','E')

 Accepted Answer

Bruno Luong
Bruno Luong on 1 Oct 2018

You probably have more than one curves in each color

hdata = plot(T3_All(mask),Tmax_All(mask),'o');
hlin = plot(T3_All,Res_Lin,'k');
hpwrd = plot(T3_All,Res_Pwr,'g');
hexp = plot(T3_All,Res_Exp,'r');
xlabel('T3 (K)'),ylabel('Residuals (K)');
h = [hdata(1) hlin(1) hpwrd(1) hexp(1)];
legend(h, 'Data','Linear','Power','Exponential','location','E')

5 Comments

Sorry Bruno, this didn't work.
Bruno Luong
Bruno Luong on 1 Oct 2018
Edited: Bruno Luong on 1 Oct 2018

What do you see on the screen when run this code? Mine gives correct legend color and linestyle

clear
close all
T3_All = 1:10;
Tmax_All = rand(length(T3_All),1);
Res_Lin = rand(length(T3_All),5);
Res_Pwr = rand(length(T3_All),5);
Res_Exp = rand(length(T3_All),5);
mask = true(size(T3_All));
figure
hold on
hdata = plot(T3_All(mask),Tmax_All(mask),'o');
hlin = plot(T3_All,Res_Lin,'k');
hpwrd = plot(T3_All,Res_Pwr,'g');
hexp = plot(T3_All,Res_Exp,'r');
xlabel('T3 (K)');
ylabel('Residuals (K)');
h = [hdata(1) hlin(1) hpwrd(1) hexp(1)];
legend(h, {'Data','Linear','Power','Exponential'})
This works here, but I don't think it will work when I apply it to my code. This is why I said, I think the problem is with my first line of code. Attached what I see.
Bruno Luong
Bruno Luong on 1 Oct 2018
Edited: Bruno Luong on 1 Oct 2018
So start with my code then replace the random data with your data one by one to see where the problem is.
I think it's somewhere in your code but you don't show us.
D.J
D.J on 1 Oct 2018
Edited: D.J on 1 Oct 2018
Guess what Bruno ! your last attempt worked perfectly OK now! I think it was the problem with the "mask". I used your last code, and replaced the "mask" with "isfinite" and it worked ! Thanks a million !

Sign in to comment.

More Answers (0)

Tags

Asked:

D.J
on 1 Oct 2018

Edited:

D.J
on 1 Oct 2018

Community Treasure Hunt

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

Start Hunting!