Adding a legend to a plot

9 次查看(过去 30 天)
Savraj Saggu
Savraj Saggu 2014-12-6
回答: Simone 2014-12-6
How do I add a legend to a plot that has three variables?
This is the code that I am working on:
y =[99;1;0];
beta = [0.005, 0.05, 0.1];
nu = [0.05, 0.075, 0.1];
options=odeset();
for i = 1:length(beta)
for index = 1:length(nu)
[T,Y] = ode45(@sir_ode,[0 30], y,options,beta(i),nu(index));
figure
plot(T,Y)
xlabel('Time(days)')
ylabel('Population(%)')
end
end
I have tried the code
legend(Variable1,Variable2, Variable3)
but that did not yield the legend.

回答(1 个)

Simone
Simone 2014-12-6
Savraj,
when using the legend function, place your variable names in single quotes, as you would for coding a plot title or for your x or y axis.
the code
legend('Variable1', 'Variable2', 'Variable3');
placed inside your nested for loop should give you your legend.

类别

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