问题
I have a set of data with a total of 4 independent variables, and I figure the only way to represent 4 independent variables and one dependent variables is to animate a 3D scatter-plot.
Let's say I have the following set of data:
W X Y Z Val
0 0 0 0 5.5
0 0 0 1 2.3
0 0 1 0 1.6
0 0 1 1 8.8
0 1 0 0 2.6
0 1 0 1 4.8
0 1 1 0 0.1
0 1 1 1 1.1
1 0 0 0 1.0
1 0 0 1 0.0
1 0 1 0 0.4
1 0 1 1 4.4
1 1 0 0 4.4
1 1 0 1 7.9
1 1 1 0 9.1
1 1 1 1 2.3
And the values were read in and converted to the following arrays:
W = {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1}
x = {0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1}
X = {0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1}
Z = {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}
Val = {5.5,2.3,1.6,8.8,2.6,4.8,0.1,1.1,1.0,0.0,0.4,4.4,4.4,7.9,9.1,2.3}
I'm wondering how to create a 3D scatter-plot (scatter3) with X, Y, and Z as the independent variables, Val as represented by the colors of the dots, and to animate with respect with the variable W as time?
Basically, when the different values of Val for when W=0 and W=1 are plotted at different times with respect to X, Y, and Z.
回答1:
Based on your edit, I have slightly refined my suggestions:
mat=cell2mat([W;x;X;Z;Val])'; %Convert cells into a matrix
colors=prism(numel(mat(:,1)));
scatter3(mat(1,2),mat(1,3),mat(1,4),100,colors(1,:),'filled');
axis tight;
for jj=1:8:numel(mat(:,1))
scatter3(mat(jj:jj+7,2),mat(jj:jj+7,3),...
mat(jj:jj+7,4),100,colors(jj:jj+7,:),'filled');
drawnow
pause(1)
end
In the above example the colors are sequentially assigned, but if you want the colors to be the same where VAL is the same, you could do something like the following:
mat=cell2mat([W;x;X;Z;Val])';
val_new=mat(:,5)/max(mat(:,5)); %0<VAL<1
scatter3(mat(1,2),mat(1,3),mat(1,4),100,[0 val_new(1) 0],'filled');
axis tight;
for jj=1:8:numel(mat(:,1))
scatter3(mat(jj:jj+7,2),mat(jj:jj+7,3),...
mat(jj:jj+7,4),100,[zeros(8,1) val_new(jj:jj+7,:) zeros(8,1)],'filled');
drawnow
pause(1)
end
Of course, both these examples assume that you will consistently have 8 entries at each time. And in the second instance there will be occasions where the differences in the colors are only very slight. If you want to actually save a video or a animated gif, just take a look at getframe
and imwrite
.
来源:https://stackoverflow.com/questions/14927621/animated-3d-scatter-plot-in-matlab