2015-02-05 121 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]