Hi I have 2 matrices A and B that are 1x98 that I want to plot using scatterplot. I also have the third matrix C that is used to separate the data.

So if A<=C, the dots should appear blue, and if A>C, the dots should appear black.

How do I set that up, and what the code should look like?

Thanks. I need this as soon as possible.

Answer by dpb
on 9 Sep 2019

Edited by dpb
on 10 Sep 2019

Accepted Answer

clr=[0 0 1; 0 0 0]; % blue, black RGB triplets

c=clr((A>C)+1,:); % set which based on lookup

scatter(A,B,[],c,'filled')

W/ revised .mat file and clarification of "who's who in the zoo", then the above does what Q? asks with

A=index_of_max_data.'; B=BP1_at_max_data.';C=S_G2_transition_point_data.';

If you instead want red instead of blue, fix up the clr rgb triplet to match desired color(s).

Chi Pham
on 10 Sep 2019

figure

clr=[0 0 1 ; 0 0 0]; % blue, black RGB triplets

c=clr((index_of_CDK2drop_escape<=S_end_escape)+1,:); % set which based on lookup

scatter(index_of_CDK2drop_escape*10/60,BP1_at_CDK2drop_escape,[],c)

legend('CDK2 drop before S/G2 transition', 'CDK2 drop before S/G2 transition');

xlabel('Absolute time (hr)')

ylabel('BP1 cumsum at CDK2 drop')

Thank you for your help. I have an additional quick question though. Why does my legend only have one type of data point? It reports that Ignoring extra legend entries.

dpb
on 10 Sep 2019

Ah! You didn't specify two legends! :)

"Why?" is because drawn this way you have only a single scatter() object set of points and legend() is hooked intimately with the number of objects drawn onto the axes. Only one and it'll only add one.

Two ways to fix -- first would be to select the two sections of the data and scatter() them separately instead of together. That takes extra logic to build the selection and two calls to scatter() initially.

The other is to create a set of dummy scatter() objects to label from the colors array as

...

hold on

hS=arrayfun(@(i) scatter(nan,nan,[],clr(i,:),'filled'),1:size(clr,1));

hL=legend(hS,'A','B','Location','northwest');

The above after you've done the previous plotting adds two scatter() objects that are invisible but match the two (or howerver many you define) colors and marker type as targets for the subsequent call to legend that uses those two object handles.

Your choice as to which way to do it...

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Fabio Freschi (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479591-scatterplot-with-condition-on-another-matrix#comment_743981

## Chi Pham (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479591-scatterplot-with-condition-on-another-matrix#comment_744041

## dpb (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479591-scatterplot-with-condition-on-another-matrix#comment_744130

## Chi Pham (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479591-scatterplot-with-condition-on-another-matrix#comment_744402

## dpb (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479591-scatterplot-with-condition-on-another-matrix#comment_744461

## dpb (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/479591-scatterplot-with-condition-on-another-matrix#comment_744487

Sign in to comment.