Live Script: Multiple Figures Merge into a Single Animation

When running a Live Script that plots several figures inside a loop, I expect MATLAB to produce separate animations, one per figure.
However, all plots are combined into a single inline animation — the frames from different figures appear alternately, making the animation look like a “blinking” mix of results.
Here’s a simplified version of my code:
a = linspace(0, pi ,100);
for ii = 1:100
figure(1); hold on; box on
plot(gca, a(ii), sin(a(ii)), 'r*')
xlim([0 pi]); ylim([-1 1]);
drawnow
figure(2); hold on; box on
plot(gca, a(ii), cos(a(ii)), 'r*')
xlim([0 pi]); ylim([-1 1]);
drawnow
end
In the Live Editor, instead of getting two separate animations (one for figure(1) and one for figure(2)), the frames from both are merged into one.
Any guidance or best practices for managing multiple animated figures in Live Scripts would be appreciated.

1 Comment

I'm facing this same issue with LiveScript playback of multiple simultaneous figures, and I'm afraid it seems to just be a limitation of how LiveScript can render and animate these things.
The only solution I can think of would be to have seperate "i" for loops.
I've attached a couple of examples where I tried to do some troubleshooting to no avail:
%% Identifying each figure individually
% create figures
Fig1 = figure;
Fig2 = figure;
% create axes
Ax1 = axes('parent',Fig1);
Ax2 = axes('parent',Fig2);
% create xdata
XData = 1:100;
% create ydata
Y1Data = rand(100,1);
Y2Data = rand(100,1);
% create plots
Y1_Plot = plot(Ax1,XData,Y1Data,'b');
Y2_Plot = plot(Ax2,XData,Y2Data,'r');
% set contant ylimits (optional)
Ax1.YLim = [0,100];
Ax2.YLim = [0,100];
% set titles (to distinguish)
title(Ax1,"1");
title(Ax2,"2");
% iterative loop (e.g. timestep)
for n = 1:100
% update ydata values
Y1Data = Y1Data + rand(100,1);
Y2Data = Y2Data + rand(100,1);
% update plots
Y1_Plot.YData = Y1Data;
Y2_Plot.YData = Y2Data;
drawnow
end
%% Using structures for n number of figures
% create xdata
XData = 1:100;
% create color array (to distinguish plots)
CFormats = ['b','r'];
for n = 1:2
% create figures
Fig(n) = figure;
% create axes
Ax(n) = axes('parent',Fig(n));
% create ydata
YData(n) = {rand(1,100)};
% create initial plots
Plot(n) = plot(Ax(n),XData,YData{n},CFormats(n));
end
% iterative loop (e.g. timestep)
for i = 1:100
for n = 1:2
% set titles (to distinguish, this is needed in the loop to circumvent LiveScript auto-update))
Ax(n).Title.String = string(n);
% set contant ylimits (optional, this is needed in the loop to circumvent LiveScript auto-update)
Ax(n).YLim = [0,100];
% update ydata values
YData{n} = YData{n} + rand(1,100);
% update plots to new ydata
Plot(n).YData = YData{n};
end
% render both figures
drawnow
end

Sign in to comment.

Answers (1)

Here's an approach with animatedline and subplot -
%Data
N = 500;
x = linspace(0, pi, N);
y1 = sin(x);
y2 = cos(x);
%Generate subplot
sub = subplot(2,1,1);
axis([0,2*pi,-1,1])
h1=animatedline('Color','r');
subplot(2,1,2)
axis([0,2*pi,-1,1])
h2=animatedline('Color','k');
%Plot lines point by point
for k=1:N
addpoints(h1,x(k),y1(k));
addpoints(h2,x(k),y2(k));
drawnow
end

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 14 Oct 2025

Commented:

on 1 May 2026 at 15:21

Community Treasure Hunt

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

Start Hunting!