What's the easiest way to create an animated scatter plot? I have a plot with over 20,000 points in my first scatter. These points shift their positions in a second scatter. I'd love to animate this shift. Is there a straightfoward way in Matlab to do this?

 Respuesta aceptada

VINAYAK LUHA
VINAYAK LUHA el 10 de Sept. de 2023
Editada: VINAYAK LUHA el 10 de Sept. de 2023

1 voto

Hi Gautam,
As per my understanding, you wish to animate the shift of scatter points between two scatter plots,
Here's a possible workaround,
xi = rand(1, 10);
xi = rand(1, 10);
yi = rand(1, 10);
xf = rand(1, 10);
yf = rand(1, 10);
figure;
ax = gca;
ax.NextPlot = 'replaceChildren';
axis([0 1 0 1]);
numFrames = 50;
for i = 1:numFrames
% Calculate intermediate data
t = i / numFrames;
xt = (1 - t) * xi + t * xf;
yt = (1 - t) * yi + t * yf;
% Plot scatter plot
scatter(xt, yt, [], [xt', yt', 1 - xt'], 'filled');
hold on;
for j = 1:numel(xt)
plot([xi(j), xt(j)], [yi(j), yt(j)], 'Color', [xt(j), yt(j), 1 - xt(j)], 'LineWidth', 1.5);
end
hold off;
frame = getframe(gcf);
im = frame2im(frame);
[imind, cm] = rgb2ind(im, 256);
% Write the indexed image to GIF file
if i == 1
imwrite(imind, cm, 'animation.gif', 'gif', 'Loopcount', inf, 'DelayTime', 0.1);
else
imwrite(imind, cm, 'animation.gif', 'gif', 'WriteMode', 'append', 'DelayTime', 0.1);
end
if i==numFrames
break
end
% Animation speed
pause(0.01);
cla
end
Hope this helps

5 comentarios

Gautam Sethi
Gautam Sethi el 10 de Sept. de 2023
Thanks Vinayak, this is super helpful.
I have two questions about the code:
  1. At the end of the code, it resets the figure to blank. I can't figure out why it does that and not end on the final coordinates.
  2. Is there a way I could convert this animation to a gif and import it in PowerPoint?
Thanks again for your help.
Walter Roberson
Walter Roberson el 10 de Sept. de 2023
xi = rand(1, 10);
yi = rand(1, 10);
xf = rand(1, 10);
yf = rand(1, 10);
figure;
ax = gca;
ax.NextPlot = 'replaceChildren';
axis([0 1 0 1]);
numFrames = 50;
for i = 1:numFrames
% Calculate intermediate data
t = i / numFrames;
xt = (1 - t) * xi + t * xf;
yt = (1 - t) * yi + t * yf;
% Plot scatter plot
scatter(xt, yt, [], [xt', yt', 1 - xt'], 'filled');
hold on;
for j = 1:numel(xt)
plot([xi(j), xt(j)], [yi(j), yt(j)], 'Color', [xt(j), yt(j), 1 - xt(j)], 'LineWidth', 1.5);
end
hold off;
if i ~= numFrames %do not clear at the end
% Animation speed
pause(0.1);
cla;
end
end
VINAYAK LUHA
VINAYAK LUHA el 10 de Sept. de 2023
Editada: VINAYAK LUHA el 10 de Sept. de 2023
Hi Gautam,
Regarding your questions -
  1. The "cla" commands at the end in the loop clears the current axis from all the graphics object, including the lines and points,hence the blank figure.If you want the animation to stop at the final points, just put a condition on "cla" command for the final frame.
  2. Yes you can convert the axis/figure to indexed images and save it as a ".gif" file.
I've updated the code as per the discussion.
Gautam Sethi
Gautam Sethi el 11 de Sept. de 2023
Wow, Vinayak, that's amazing! I can't thank you enough for writing ths code.
Gautam Sethi
Gautam Sethi el 11 de Sept. de 2023
Thank you, Walter. I appreciate your assistance. I have a better understanding of writing animations now.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Animation en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Sept. de 2023

Comentada:

el 11 de Sept. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by