2015-02-05 173 views
2

我是Matlab新手,我試圖將散點圖繪製爲繪製座標軸中的4個點。依次更改散點圖中每個點的顏色

例如

x = [0;0;1;-1]; 
y = [1;-1;0;0]; 
scatter(x,y); 

什麼我想要做的是改變上述情節不斷協調順時針方向

enter image description here

像上面的PIC的顏色之一。

如果不是有另一種方法,我可以這樣做嗎?

在此先感謝。

回答

1

您需要單獨繪製的每個點,得到一個處理每一個,然後改變自己的'color'財產順序在一個循環:

%// Data 
x = [-1;0;1;0]; %// define in desired (counterclockwise) order 
y = [0;1;0;-1]; 
color1 = 'g'; 
color2 = 'r'; 

%// Initial plot 
N = numel(x); 
h = NaN(1,N); 
hold on 
for n = 1:N 
    h(n) = plot(x(n), y(n), 'o', 'color', color1); 
end 
axis([-1.2 1.2 -1.2 1.2]) %// set as desired 

%// Change color of one point at a time, and restore the rest 
k = 0; 
while true 
    k = k+1; 
    pause(.5) 
    n = mod(k-1,N)+1; 
    set(h(n), 'color', color2); 
    set(h([1:n-1 n+1:end]), 'color', color1); 
end 

enter image description here

3

您可以以設置顏色添加第四個參數來scatter(第三參數設置的大小,你可以讓它空):

col = lines(4); % create 4 colors using the 'lines' colormap 
scatter(x,y,[],col); 

您可以使用一些其他的色彩映射表(類型doc colormap Matlab獲取更多細節),或者只需輸入一些數字矢量即可使用當前的顏色映射。

編輯我剛剛意識到你只想改變一點的顏色;您可以使用(例如)col = [2 1 1 1]