如何通过在R或MATLAB中一次添加每个点来为3D散点图制作动画

时间:2019-04-24 08:14:10

标签: r matlab animation ggplot2 gganimate

我有一组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将每个点顺序添加到该点。首先添加由第一行表示的点,然后再添加由第二行表示的点,一直到最后一个点。

此外,我希望控制添加点的速度。

对于2D散点图,我可以使用以下代码:

 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散点图执行此操作的信息。谁能告诉我该怎么做?谢谢。

1 个答案:

答案 0 :(得分:2)

这是MATLAB的答案

通常,可以按照相同的方法对图(或3d图,散点图,表面或其他图形对象)进行动画处理:

  • 执行第一个plot / plot3 / scatter / surf,并检索其句柄。第一个图可以包含第一个“初始”点集,甚至可以是空的(使用NaN值创建具有 invisible 数据点的图)。
  • 设置轴限制和所有其他固定的可视化选项(视点,摄像机角度,闪电...)。无需设置动画过程中会引起争议的选项。
  • 在循环中,更新一组最小的绘图对象属性:XDataYData(如果是3D绘图,则为ZData,如果绘图对象中有一些且您是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

这将产生:

enter image description here

相关问题