MATLAB:如何制作不同颜色的点的二维图?

时间:2016-12-14 04:38:25

标签: matlab plot colors scatter-plot points

我想制作具有不同x,y坐标的点的2D绘图,并根据单独的变量具有颜色。我已经为x和y坐标创建了列向量,而另一列包含1或-1。我想用1表示红色,-1表示蓝点。我的代码如下:

x_dis=rho_rec(1:nDis,xCol,step);%x coordinates
y_dis=rho_rec(1:nDis,yCol,step); %y coordinates 
bv=rho_rec(1:nDis,bvCol,step); % 1 or -1

for i=1:1:nDis
    if bv(i)==1
        dis_color(i,1:3)=[0 0 1]; %blue
    elseif bv(i)==-1
        dis_color(i,1:3)=[1 0 0]; %red
    end
end

plot(x_dis,y_dis,'.','Color',dis_color(1:nDis,:))

然而它并不起作用。我该如何修改代码?谢谢。

1 个答案:

答案 0 :(得分:2)

我相信你的意思是制作散点图。您需要使用分散功能。如果是这种情况,则不传递1 `HCons` True `HCons` HNil :: HList '[Int, Bool] 参数。

以下内容对您有用:

'Color'

scatter(x_dis,y_dis,[],dis_color(1:nDis,:),'.') 是标记大小的占位符

你在其他地方初始化[]吗?您可能希望在for循环之前执行此操作。