Update legend to current plot

2 次查看(过去 30 天)
kyrresc
kyrresc 2016-2-29
回答: KSSV 2016-3-1
I'm writing a code where I have one constant dataset that stays in the plot the entire time while the other datasets pop up in the plot one by one.(I'm deleting the pop-up dataset-plot after every iteration; which means that I at every time only have two plots in my figure - the constant dataset and the current pop-up dataset). As far as I know you can't have multiple legends in a matlab plot, so I want to make a legend for the pop-up datasets that updates after every iteration (i.e legend changing color in accordance to plot). To delete the plots i'm simply using the delete(plot) command however I haven't figured out if you can do anything similar with legends

回答(1 个)

KSSV
KSSV 2016-3-1
% constant plot
t = linspace(0,2*pi) ;
x = 0.5*sin(t) ;
for i = 1:100
plot(t,x,'r') ; % plot constant sine function
hold on
% Generating a random varying second function
dx = 0.1*randn(size(t)) ;
x2 = x+dx ; % A varying function
plot(t,x2,'color',rand(1,3)) ; % plot second varying function
hold off
legend('sin function','varying function')
drawnow
end

类别

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