不使用保持(或全部)的不同颜色散点

Lit*_*les 0 matlab scatter colors scatter-plot matlab-figure

我想使用三组数据在MATLAB中创建一个散点图.X,Yc.X并且Y是它们各自的轴图,但是c在每个散点点分类上保存信息(整数值).我希望将每个分类图作为单独的颜色.这些整数值很简单,可以转换成相应的颜色选择,所以没有问题.目前,C作为我的颜色选择,我正在使用,

    hold on
for k=1:K
    scatter(X(c==k,:),Y(c==k),[],C(k,:),'filled');
end
Run Code Online (Sandbox Code Playgroud)

我对这种动机是希望建立一个UpdateFcnDataCursorManager为了显示与数据光标每个点的日期.我无法使用多个散点图进行此操作,并且这是解决问题的最简单方法.

Hok*_*oki 5

正如评论中所说,scatter可以采用代表颜色的第四个参数.第三个参数(您c为每个散点图使用的参数)仅控制大小.

对你来说,打电话的方式scatter应该是: scatter(x,y, size, colour , 'filled')

请仔细阅读文档,scatter以便更好地了解其用法.

下面是如何使用4个参数的快速示例.我不得不创建样本数据,因为你没有指定任何(我选择在x轴上有日期,我假设你想要所有组的相同大小......但是根据你的需要进行调整).

请注意,分散对象具有名为的属性CData.这是相同的大小,x它包含每个数据点的颜色(以及图颜色图中颜色的索引).这就是我们用来了解datatip函数中你的点的分类.CData如果要以交互方式更改颜色,可以直接重新调整此向量.

function h = scatter_datatip_demo

%// basic sample data
npts = 50 ;
nClass = 12 ; %// let's say we have 12 different class
x = round(now) + randi([-10 10],npts,1) ;
y = rand(size(x)) ;
s = ones(size(x))*40 ;
c = randi([1 nClass],size(x)); %// randomly assign a class for each point

%// Draw a single scatter plot (specifying the colour as the 4th input)
h.f = figure ;
h.p = scatter(x,y , s , c , 'filled') ; %// <== Note how scatter is called
colormap( jet(nClass) ) ;

%// add custom datatip function
set( datacursormode(h.f) , 'UpdateFcn',@customDatatipFunction );


function output_txt = customDatatipFunction(~,evt)
    pos = get(evt,'Position');

    hp = handle( get(evt,'Target') ) ;           %// get the handle of the scatter plot object
    ptClass = hp.CData( get(evt,'DataIndex') ) ; %// get the colour index of the current point

    output_txt = { ...
        'My custom datatip' , ...
        ['Date : ' , datestr(pos(1))  ] ...
        ['Class: ' , num2str(ptClass) ] ...
        ['Value: ' , num2str(pos(2),8)] ...
                };
Run Code Online (Sandbox Code Playgroud)

demoimg