matlab - Different Colour Scatter points without using Hold on (or all) -


i create scatter plot in matlab using 3 sets of data. x,y , c. x , y respective axis plots c holds information (integer values) on each scatter points classification. wish make each classification plot separate colour. these integer values simple enough convert respective colour choices no problem there. currently, c colour choice, using,

    hold on k=1:k     scatter(x(c==k,:),y(c==k),[],c(k,:),'filled'); end 

my motivation wish create updatefcn in datacursormanager in order show dates of each point data cursor. have been unable multiple scatter plots , figure simplest way problem.

as told in comment, scatter can take 4th argument represent color. 3rd argument (the 1 use c each of scatter plot), controls size.

for you, way call scatter should be: scatter(x,y, size, colour , 'filled')

read documentation scatter understand usage better.

below quick example on how use 4 parameters. had create sample data since didn't specify (i chose have dates in x axis, , assumed wanted same size groups ... adjust needs).

note scatter object has property named cdata. same size of x , contains colour each data point (well index of colour in figure colormap). use know classification of point in datatip function. can directly readjust cdata vector if want change colour interactively.

function h = scatter_datatip_demo  %// basic sample data npts = 50 ; nclass = 12 ; %// let's 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 class each point  %// draw single scatter plot (specifying colour 4th input) h.f = figure ; h.p = scatter(x,y , s , c , 'filled') ; %// <== note how scatter 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') ) ;           %// handle of scatter plot object     ptclass = hp.cdata( get(evt,'dataindex') ) ; %// colour index of current point      output_txt = { ...         'my custom datatip' , ...         ['date : ' , datestr(pos(1))  ] ...         ['class: ' , num2str(ptclass) ] ...         ['value: ' , num2str(pos(2),8)] ...                 }; 

demoimg


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -