2015-12-20 86 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