根据距原点的距离更改 3D Matlab Plot 上数据点的颜色
Change colour of data points on 3D Matlab Plot based on distance from origin
我想在 3d MATLAB 绘图中绘制 x、y、z 位置数据,但是我想根据数据点与原点的距离来更改数据点的颜色。作为此脚本的一部分,我当前的代码是:
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
当然,这只是一直用单一颜色绘制数据点,这不是我想要的。
我应该注意到我是在二维图中实现的,尽管离散的颜色变化不是连续的,如下所示:
%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
但是上面的方法似乎只适用于 2d 图,如果我尝试类似的代码,我将 plot 交换到 plot3,出于某种原因我仍然没有得到 3d 图。
如上所述,您可以使用散点图。
X3D=rand(1, 1000);
Y3D=rand(1, 1000);
Z3D=rand(1, 1000);
%define colors as distance to the origin
C = sqrt(X3D.^2+Y3D.^2+Z3D.^2);
%scale the colors to 0<=C<=1
C = C/max(C);
figure
% Initialise plot, get handle to object and set style to dots
h = scatter3(NaN,NaN,NaN);
colormap(jet);
% Fix axes
axis([min(X3D(:)) max(X3D(:)) min(Y3D(:)) max(Y3D(:)) min(Z3D(:)) max(Z3D(:))]);
pointSize = 3;
% 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), 'SizeData', pointSize, 'CData', C(1:ii));
end
结果如下:
我想在 3d MATLAB 绘图中绘制 x、y、z 位置数据,但是我想根据数据点与原点的距离来更改数据点的颜色。作为此脚本的一部分,我当前的代码是:
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
当然,这只是一直用单一颜色绘制数据点,这不是我想要的。
我应该注意到我是在二维图中实现的,尽管离散的颜色变化不是连续的,如下所示:
%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
但是上面的方法似乎只适用于 2d 图,如果我尝试类似的代码,我将 plot 交换到 plot3,出于某种原因我仍然没有得到 3d 图。
如上所述,您可以使用散点图。
X3D=rand(1, 1000);
Y3D=rand(1, 1000);
Z3D=rand(1, 1000);
%define colors as distance to the origin
C = sqrt(X3D.^2+Y3D.^2+Z3D.^2);
%scale the colors to 0<=C<=1
C = C/max(C);
figure
% Initialise plot, get handle to object and set style to dots
h = scatter3(NaN,NaN,NaN);
colormap(jet);
% Fix axes
axis([min(X3D(:)) max(X3D(:)) min(Y3D(:)) max(Y3D(:)) min(Z3D(:)) max(Z3D(:))]);
pointSize = 3;
% 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), 'SizeData', pointSize, 'CData', C(1:ii));
end
结果如下: