Animated 3D Scatter Plot

41 ビュー (過去 30 日間)
Fabio Taccaliti
Fabio Taccaliti 2022 年 4 月 20 日
コメント済み: Star Strider 2022 年 4 月 20 日
Hello,
I would like to create an animated 3D scatter plot that is plotting the points included in the sub-array of a cell DD (102x1 cell), each sub-array is a 29x4 (where the last three column are the 3 coordinates (x,y.z)). Here below an example of my cell and sub-array.
M = [[0;0.2;0.2;0.4;0.6;0.6;0.6],rand(7,3)]
D = diff(find([1;diff(M(:,1));1]));
DD = mat2cell(M,D,4);
DD{:}
The animated scatter should display together each subarray points and then in a subsequent time step the next subarray points.
Here below the code that alreay plot all the points together.
figure(1)
for i = 1:numel(DD)
hold on; grid on; grid minor; axis equal;
set(gcf, 'Color', 'White');
set(gca, 'Fontsize', 12);
set(gca, 'ZDir','reverse')
scatter3(DD{i}(:,2),DD{i}(:,3),DD{i}(:,4))
view(3)
xlabel('x [mm]')
ylabel('y [mm]')
zlabel('z [mm]')
end
Thanks in advance

採用された回答

Star Strider
Star Strider 2022 年 4 月 20 日
Try this slightly augmented version —
figure(1)
hold on
grid minor
axis([0 2 0 2 0 2])
for i = 1:numel(DD)
view(3)
set(gcf, 'Color', 'White');
set(gca, 'Fontsize', 12);
set(gca, 'ZDir','reverse')
scatter3(DD{i}(:,2),DD{i}(:,3),DD{i}(:,4))
grid on
axis([0 2 0 2 0 2])
drawnow
pause(0.25)
xlabel('x [mm]')
ylabel('y [mm]')
zlabel('z [mm]')
end
hold off
It fixes the axis limits and adds drawnow and pause to create the animation.
Experiment to get the desired results.
.
  6 件のコメント
Fabio Taccaliti
Fabio Taccaliti 2022 年 4 月 20 日
Thanks a lot, now it works :)
Star Strider
Star Strider 2022 年 4 月 20 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 4 月 20 日
Rather than recreating the scatter plot each time, I'd apply the first of the techniques listed on the Animation Techniques documentation page. If I ran this in MATLAB Answers you wouldn't see the animation, but if you run it in MATLAB you can.
% Sample data
theta = 0:15:360;
x = cosd(theta);
y = sind(theta);
% Create the initial "frame" of the animation
h = scatter(x, y, 'o');
% Control the axes so at its "widest" the whole circle still fits
axis([-5 5 -5 5])
% Make it look circular
axis equal
% At each frame, push each point outward (or pull it inwards)
for r = repmat([1:5 4:-1:2], 1, 10)
% Update the existing object's properties rather than creating a new one
h.XData = r*x;
h.YData = r*y;
% Let you see the animation
pause(0.1)
end
In the "real" animation you might want to use one of the options for drawnow instead of pause.
  1 件のコメント
Fabio Taccaliti
Fabio Taccaliti 2022 年 4 月 20 日
Thanks Steven, I'll have a better look into this :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by