Lucas Santana Souza
Lucas Santana Souza

Reputation: 1

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

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')

Notice the legend does not match the color and shape in the graph

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'})
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')

Upvotes: 0

Views: 135

Answers (1)

mimocha
mimocha

Reputation: 1121

The problem is the shape of your data. Your data for the y-axis, XIR, Xir, ... are all matrices.

So when you use the command: plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*'), you are getting more than just one data series.

Try this out and you will see what I mean:

plot(eStart:eStep:eEnd, XIR(:,transientCut:T),'g*')
legend

Where you were supposedly trying to plot one data series XIR, there will be 201 series instead. (size of eStart:eStep:eEnd is a [1,101] vector, while size of XIR(:,transientCut:T) is a [101,201] matrix).


To fix this, you have two options.

  1. Sort out how you want to plot your data. So convert the datasets like XIR(:,transientCut:T) into a vector when plotting.

  2. If you really need to plot it with that code you have, you can "trick" matlab, by plotting an empty dataset with the same markers before your dataset.

figure
hold on
plot(NaN, NaN, 'g*') % Same marker style as XIR
plot(NaN, NaN, 'b.') % Same marker style as XiR
plot(NaN, NaN, 'r.') % Same marker style as XIr
plot(NaN, NaN, 'k.') % Same marker style as Xir

% Plot your data as normal
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.')

% Call legend after plotting your data
legend('XIR','XiR','XIr','Xir')

P.S. I recommend that you rename your variables too. XIR, XiR, XIr, Xir are all too easy to switch up, as can be seen in your last code snippet:

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.'); <-- XIr, not Xir?
p4=plot(eStart:eStep:eEnd, Xir(:,transientCut:T),'k.');

Upvotes: 1

Related Questions