MATLAB: When specifying the color of points, legend does not match

legendmismatchplot

I am using MATLAB R2017b
This section of the code is generating the data to be plotted:
% Parameters
cE = 0.1;
cs = 0.1;
a0 = 0.5;
a = 0.5;
L= 0.1;
b=1
% Parameter in inspection
eStart=0.0; %inicial value of parameter
eStep=0.01; %how far apart is each value
eEnd=1; %Final value of parameter
e = eStart:eStep:eEnd; %Array containing all parameters
nPoints = length(e); %number of parameter values used
T=700; % time interval
transientCut = 500; %indicates the cut to avoid seeing transient
% points (points before reach equilibtium);
%thus as transientCut increases the graph will start to
%show points that did not reach equilibrium yet
%setting the matrix that will save the time series for each value of parameter used
XIR=zeros(nPoints,T);
XiR=zeros(nPoints,T);
XIr=zeros(nPoints,T);
Xir=zeros(nPoints,T);
for k=1:nPoints %outer loop gives the condition in which the eValues
% will be accesed
xiR =0.5 %IC


xIr =0.20 %IC
xir = 0.3 %IC
xIR= 1-xiR-xIr-xir;
for t=1:T
%Difference equation
xIR=(xIR*(1 + (a0 + a*(L*xIr + xIR))*(-cE - cs + (1 - xir - xIr + (xir + xIr)*e(k))^b)))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
xiR=(xiR + xiR*(a0 + a*(L*xIr + xIR))*(-cE + (1 - xir - xIr + (xir + xIr)*e(k))^b))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
xIr=(xIr*(1 - a0*(cE + cs) + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*e(k)^b))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
xir=(xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*e(k)^b))/(xiR + xIr + xIR - cE*xiR*(a0 + a*(L*xIr + xIR)) - (cE + cs)*(a0*(xIr + xIR) + a*xIR*(L*xIr + xIR)) + xir*(1 - a0*cE + (a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b) + xIr*(a0 + a*(L*xIr + xIR))*((-1 + xir + xIr)*(-1 + e(k)))^b + a0*(xir + xIr)*e(k)^b + (xiR + xIR)*(a0 + a*(L*xIr + xIR))*(1 - xir - xIr + (xir + xIr)*e(k))^b);
%saving for each value of e (at row k), the time serie
XIR(k,t)=xIR;
XiR(k,t)=xiR;
XIr(k,t)=xIr;
Xir(k,t)=xir;
end
end
Here is where the problem is.
If I let the MATLAB default choose the colors the legend matches the color.
But if I specify the shape and color of the data, then legend does not match these specifications (see attached below)
figure
plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*' ,eStart:eStep:eEnd, XiR(:,transientCut:T),'b.' ...
,eStart:eStep:eEnd, XIr(:,transientCut:T),'r.' ,eStart:eStep:eEnd, Xir(:,transientCut:T),'k.')
xlabel('xlable','FontSize',12)
ylabel('ylable','FontSize',12)
ylim([0 1])
title(['c_{s}=',num2str(cs),', c_{E}=',num2str(cE),...
', L=',num2str(L),', a_{0}=',num2str(a0), ', a=',num2str(a)])
legend('XIR','XiR','XIr','Xir')
I also tried suggestions of similar issues that I found in this forum, but the legend still does not match the color/shape specifications:
p1=plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*');
hold on
p2=plot(eStart:eStep:eEnd, XiR(:,transientCut:T),'b.');
p3=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'r.');
p4=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'k.');
xlabel('xlable','FontSize',12)
ylabel('ylable','FontSize',12)
ylim([0 1])
title(['c_{s}=',num2str(cs),', c_{E}=',num2str(cE),...
', L=',num2str(L),', a_{0}=',num2str(a0), ', a=',num2str(a)])
legend([p1;p2;p3;p4], {'xIR','xiR','xIr','xir'})

Best Answer

One approach is to add a handle to the plot call (note the semicolon at the end to prevent printing all of the handles to the Command Window):
hp = plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*' ,eStart:eStep:eEnd, XiR(:,transientCut:T),'b.' ...
,eStart:eStep:eEnd, XIr(:,transientCut:T),'r.' ,eStart:eStep:eEnd, Xir(:,transientCut:T),'k.');
then use the appropriate parts of it in the legend call:
legend(hp(1:201:end),'XIR','XiR','XIr','Xir')
That appeared to work correctly when I ran it (in R2020a, Update 4). (There are 804 Line objects in the plot, so a step size of 201 will get the first instance of each one.)
EDIT — (18 Jul 2020 aat 19:20)
Added plot image —
.