I have x,y,z position data that I want to plot in a 3d MATLAB plot however I want to alter the colour of the data points based on their distance from the origin. My current code as part of this script is:
figure
% Initialise plot, get handle to object and set style to dots
h = plot3(NaN,NaN,NaN,'.');
% Fix axes
axis([min(X3D(:)) max(X3D(:)) min(Y3D(:)) max(Y3D(:)) min(Z3D(:)) max(Z3D(:))]);
% Loop over all elements of XS3D
for ii = 1:length(X3D)
% pause for animation behaviour
pause(0.01)
% Set data of graph
set(h, 'XData', X3D(1:ii), 'YData', Y3D(1:ii), 'ZData', Z3D(1:ii));
end
Of course, this only plots the data points with a single colour at all times which is not what I want.
I should note that I achieved this in a 2D plot, albeit with discrete colour changes not continuous, as shown below:
%Plot starts here
figure
% Set x and y limits of the plot
xlim([min(X(:))-1 max(X(:))+1])
ylim([min(Y(:))-1 max(Y(:))+1])
% Plot point by point
for k = 1:numel(X)
if (X(k)^2 + Y(k)^2) < 500
plot(X(k),Y(k),'.g')
elseif (X(k)^2 + Y(k)^2) >= 500 && (X(k)^2 + Y(k)^2 < 1000)
plot(X(k), Y(k),'.','color',orange)
else
plot(X(k), Y(k), '.r')
end
% MATLAB pauses for 0.001 sec before moving on to execute the next
% instruction => thus creating animation effect
pause(0.001);
end
However the above only seems to work for 2d plots, if I try code similar to this where I swap plot to plot3 I still do not get a 3d plot for some reason.
scatter3
. With the fifth input argumentC
you can specify options for the color. – mikkola