2015-12-20 57 views
1

我想要在三维MATLAB绘图中绘制x,y,z位置数据,但是我想根据它们的数据点来改变数据点的颜色距离原点的距离。我当前的代码,因为这剧本的部分是:更改3D Matlab上数据点的颜色基于原点距离的绘图

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 

当然,这只有在任何时候这不是我想要的地块用单一颜色的数据点。

我要指出,我在2D情节实现这一点,尽管有离散的颜色变化不连续的,如下图所示:

%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 plot。

+6

你应该看看['scatter3'(http://se.mathworks.com/help/matlab/ref/scatter3.html) 。通过第五个输入参数'C',您可以指定颜色的选项。 – mikkola

回答

2

如上所述,您可以使用scatter3图。

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 

下面是结果:

enter image description here

+0

太棒了!这正是我需要的,非常感谢! – ArchbishopOfBanterbury