ignoring extra legend entries

84 次查看(过去 30 天)
Refilwe Matlou
Refilwe Matlou 2022-2-28
clear all
close all
clc
Pi = 27;
mu = 1/(66.1*365); delta1 = 0.32; delta2 = 0.12;
K = 5000; rho = 0.2; epsilon = 0.075;
sigma1 = 0.125; sigma2 = 0.125; gamma1 = 0.025;
gamma2 = 0.05; omega1 = 0.0153; xi1 = 0.2445;
xi2 = 0.03315; varphi = 0.035; phi = 0.0035;
beta1 = 0.55;
fdefun = @(t,y)[Pi + omega1*y(6) - beta1*y(7)/(y(7)+K)*y(1) - mu*y(1);
beta1*y(7)/(y(7)+K)*y(1) - (epsilon+mu)*y(2);
rho*epsilon*y(2) - (mu+sigma1+gamma1)*y(3) + sigma2*y(4);
(1-rho)*epsilon*y(2) + sigma1*y(3) - (delta1+mu+sigma2+phi)*y(4);
phi*y(4) - (gamma2+delta2+mu)*y(5);
gamma2*y(5) + gamma1*y(3) - (mu+omega1)*y(6);
xi1*y(3) + xi2*y(4) - varphi*y(7)];
alpha = 1;
alpha = 0.9;
alpha = 0.8;
t0 = 0 ; tfinal = 100; y0 = [10000;500;20;10;0;0;50000];
h = 2^(-6) ;
[t, y_fde12] = fde12(alpha,fdefun,t0,tfinal,y0,h);
figure(4)
plot(t,y_fde12(3,:),'k-','Linewidth',2) ;
plot(t,y_fde12(3,:),'r--','Linewidth',2) ;
plot(t,y_fde12(3,:),'b-.','Linewidth',2) ;
hold on
xlabel('Time (days) ') ; ylabel('Asymptomatic infected individuals') ;
legend('\alpha = 1','\alpha = 0.9','\alpha = 0.8' ) ;
title('\gamma_2 =0.05');
figure(5)
%plot(t,y_fde12(4,:),'k-','Linewidth',2) ;
plot(t,y_fde12(4,:),'r--','Linewidth',2) ;
%plot(t,y_fde12(4,:),'b-.','Linewidth',2) ;
hold on
xlabel('Time (days) ') ; ylabel('Symptomatic infected individuals') ;
legend('\alpha = 1','\alpha = 0.9','\alpha = 0.8' ) ;
title('\gamma_2= 0.05');

回答(1 个)

Voss
Voss 2022-3-1
The warning "Ignoring extra legend entries" happens if you call legend() with more names than there are lines in your axes. In this case, it is related to your use of "hold on".
Before "hold on" is called, each call to plot() deletes any previous line(s), so when you call "hold on", only the last line plotted (if any) exists and is "held onto". So when you plot three lines without having hold on and then call legend() with three names, legend only knows about the last line (since that's the only one that exists), so it gives you the warning because you gave it three names for one line.
The solution is to call hold on right after (or right before) the first line is plotted in each figure (instead of calling hold on after multiple lines are plotted). And also, when you change which lines are plotted (e.g., by commenting out the plot command) you should also change the corresponding names you send to legend().
% This is ok:
figure();
hold on
plot(1:10);
plot(2:20);
legend('a','b');
% This is also ok:
figure();
plot(1:10);
hold on
plot(2:20);
legend('a','b');
% This gives the warning:
figure();
plot(1:10); % you will not see the line shown right below here if you run this ...
plot(2:20); % ... because this deletes the previous plotted line
hold on % holds just the last line
legend('a','b'); % note that the 2nd line (2:20) is now called 'a' in the legend
Warning: Ignoring extra legend entries.
  2 个评论
Steven Lord
Steven Lord 2022-7-21
Another technique you may want to use, to put the label information closer in the code to the plot call, is to set the DisplayName property of the line as it's created just like you set the LineWidth property. Then your legend call doesn't need to receive the labels as input; you just need to tell MATLAB to show the legend. [If you only wanted to show some of the lines in the legend, you could do that by passing the handles to those lines into legend.]
% Set up the sample data
x = 0:360;
% Set up the axes so I can create multiple plots on it
axis([0 360 -1 1])
hold on
% Plot -- label information specified here
plot(x, sind(x), 'DisplayName', 'Sine') % Label information here
plot(x, cosd(x), 'DisplayName', 'Cosine') % and here
% Display legend -- no label information needed here
legend show

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by