如何通过在 R 或 MATLAB 中一次添加每个点来制作 3D 散点图动画
How to animate 3D scatter plot by adding each point at a time in R or MATLAB
我有一组 3D 坐标 here。数据有 52170 行和 4 列。每行代表一个点。第一列是点索引号,从 1 增加到 52170。第二列到第四列分别是 x、y 和 z 轴的坐标。前10行如下:
seq x y z
1 7.126616 -102.927567 19.692112
2 -10.546907 -143.824966 50.77417
3 7.189214 -107.792068 18.758278
4 7.148852 -101.784027 19.905006
5 -14.65788 -146.294952 49.899158
6 -37.315742 -116.941185 12.316169
7 8.023512 -103.477882 19.081482
8 -14.641933 -145.100098 50.182739
9 -14.571636 -141.386322 50.547684
10 -15.691803 -145.66481 49.946281
我想创建一个 3D 散点图,其中使用 R 或 MATLAB 将每个点按顺序添加到该图中。先添加第一行代表的点,再添加第二行代表的点,...,一直到最后一个点。
另外,我想控制加点的速度
对于二维散点图,我可以使用以下代码:
library(gganimate)
x <- rnorm(50, 5, 1)
y <- 7*x +rnorm(50, 4, 4)
ind <- 1:50
data <- data.frame(x, y, ind)
ggplot(data, aes(x, y)) + geom_point(aes(group = seq_along(x))) + transition_reveal(ind)
但我找不到有关如何为 3D 散点图执行此操作的信息。谁能告诉我如何做到这一点?谢谢。
这是 MATLAB
的答案
在一般情况下,动画图(或 3d 图、散点图、曲面或其他图形对象)可以按照相同的方法完成:
- 执行第一个 plot/plot3/scatter/surf,并检索其句柄。第一个图可以包含第一个 "initial" 组点,甚至可以为空(使用
NaN
值创建具有 invisible 数据点的图)。
- 设置轴限制和所有其他将被修复的可视化选项(视角、摄像机角度、闪电...)。无需设置动画过程中会演化的选项。
- 在循环中,更新绘图对象属性的最小集合:
XData
、YData
(ZData
如果是 3D 绘图,CData
如果绘图对象有一些并且您想为颜色设置动画)。
下面的代码是上述方法的实现,适用于您的情况:
%% Read data and place coordinates in named variables
csvfile = '3D scatter plot.csv' ;
data = csvread(csvfile,2) ;
% [optional], just to simplify notations further down
x = data(:,2) ;
y = data(:,3) ;
z = data(:,4) ;
%% Generate empty [plot3] objects
figure
% create an "axes" object, and retrieve the handle "hax"
hax = axes ;
% create 2 empty 3D point plots:
% [hp_new] will contains only one point (the new point added to the graph)
% [hp_trail] will contains all the points displayed so far
hp_trail = plot3(NaN,NaN,NaN,'.b','Parent',hax,'MarkerSize',2) ;
hold on
hp_new = plot3(NaN,NaN,NaN,'or','Parent',hax,'MarkerSize',6,'MarkerEdgeColor','r','MarkerFaceColor','g','LineWidth',2) ;
hold off
%% Set axes limits (to limit "wobbling" during animation)
xl = [min(x) max(x)] ;
yl = [min(y) max(y)] ;
zl = [min(z) max(z)] ;
set(hax, 'XLim',xl,'YLim',yl,'ZLim',zl)
view(145,72) % set a view perspective (optional)
%% Animate
np = size(data,1) ;
for ip=1:np
% update the "new point" graphic object
set( hp_new , 'XData',x(ip), 'YData',y(ip), 'ZData',z(ip) )
% update the "point history" graphic object
% we will display points from index 1 up to the current index ip
% (minus one) because the current index point is already displayed in
% the other plot object
indices2display = 1:ip-1 ;
set(hp_trail ,...
'XData',x(indices2display), ...
'YData',y(indices2display), ...
'ZData',z(indices2display) )
% force graphic refresh
drawnow
% Set the "speed"
% actually the max speed is given by your harware, so we'll just set a
% short pause in case you want to slow it down
pause(0.01) % <= comment this line if you want max speed
end
这将产生:
我有一组 3D 坐标 here。数据有 52170 行和 4 列。每行代表一个点。第一列是点索引号,从 1 增加到 52170。第二列到第四列分别是 x、y 和 z 轴的坐标。前10行如下:
seq x y z
1 7.126616 -102.927567 19.692112
2 -10.546907 -143.824966 50.77417
3 7.189214 -107.792068 18.758278
4 7.148852 -101.784027 19.905006
5 -14.65788 -146.294952 49.899158
6 -37.315742 -116.941185 12.316169
7 8.023512 -103.477882 19.081482
8 -14.641933 -145.100098 50.182739
9 -14.571636 -141.386322 50.547684
10 -15.691803 -145.66481 49.946281
我想创建一个 3D 散点图,其中使用 R 或 MATLAB 将每个点按顺序添加到该图中。先添加第一行代表的点,再添加第二行代表的点,...,一直到最后一个点。
另外,我想控制加点的速度
对于二维散点图,我可以使用以下代码:
library(gganimate)
x <- rnorm(50, 5, 1)
y <- 7*x +rnorm(50, 4, 4)
ind <- 1:50
data <- data.frame(x, y, ind)
ggplot(data, aes(x, y)) + geom_point(aes(group = seq_along(x))) + transition_reveal(ind)
但我找不到有关如何为 3D 散点图执行此操作的信息。谁能告诉我如何做到这一点?谢谢。
这是 MATLAB
的答案在一般情况下,动画图(或 3d 图、散点图、曲面或其他图形对象)可以按照相同的方法完成:
- 执行第一个 plot/plot3/scatter/surf,并检索其句柄。第一个图可以包含第一个 "initial" 组点,甚至可以为空(使用
NaN
值创建具有 invisible 数据点的图)。 - 设置轴限制和所有其他将被修复的可视化选项(视角、摄像机角度、闪电...)。无需设置动画过程中会演化的选项。
- 在循环中,更新绘图对象属性的最小集合:
XData
、YData
(ZData
如果是 3D 绘图,CData
如果绘图对象有一些并且您想为颜色设置动画)。
下面的代码是上述方法的实现,适用于您的情况:
%% Read data and place coordinates in named variables
csvfile = '3D scatter plot.csv' ;
data = csvread(csvfile,2) ;
% [optional], just to simplify notations further down
x = data(:,2) ;
y = data(:,3) ;
z = data(:,4) ;
%% Generate empty [plot3] objects
figure
% create an "axes" object, and retrieve the handle "hax"
hax = axes ;
% create 2 empty 3D point plots:
% [hp_new] will contains only one point (the new point added to the graph)
% [hp_trail] will contains all the points displayed so far
hp_trail = plot3(NaN,NaN,NaN,'.b','Parent',hax,'MarkerSize',2) ;
hold on
hp_new = plot3(NaN,NaN,NaN,'or','Parent',hax,'MarkerSize',6,'MarkerEdgeColor','r','MarkerFaceColor','g','LineWidth',2) ;
hold off
%% Set axes limits (to limit "wobbling" during animation)
xl = [min(x) max(x)] ;
yl = [min(y) max(y)] ;
zl = [min(z) max(z)] ;
set(hax, 'XLim',xl,'YLim',yl,'ZLim',zl)
view(145,72) % set a view perspective (optional)
%% Animate
np = size(data,1) ;
for ip=1:np
% update the "new point" graphic object
set( hp_new , 'XData',x(ip), 'YData',y(ip), 'ZData',z(ip) )
% update the "point history" graphic object
% we will display points from index 1 up to the current index ip
% (minus one) because the current index point is already displayed in
% the other plot object
indices2display = 1:ip-1 ;
set(hp_trail ,...
'XData',x(indices2display), ...
'YData',y(indices2display), ...
'ZData',z(indices2display) )
% force graphic refresh
drawnow
% Set the "speed"
% actually the max speed is given by your harware, so we'll just set a
% short pause in case you want to slow it down
pause(0.01) % <= comment this line if you want max speed
end
这将产生: