MATLAB Answers


Scatterplot with condition on another matrix

Asked by Chi Pham on 9 Sep 2019
Latest activity Edited by dpb
on 11 Sep 2019
Accepted Answer by dpb
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.


I am sorry about that but A,B,C are just example names. Please see the data mat file. A is index_of_max_data, B is BP1_at_max_data and C is S_G2_transition_point_data.
I really appreciate your help
on 10 Sep 2019
Well, we only know what you tell us...we don't know the magic correlations. :)
See updated Answer...
on 10 Sep 2019
"but it does not seem like it separates the 2 conditions"
Because the syntax is incorrect for the if test. You've written the two variables w/o the subscript which returns a logical vector of the same size as they. if is true iff all elements of the condition are true; see the documenation for IF for details.
Also, even with that corrected, you've done the same thing in the argument to scatter; each time you call it, it will do the full vectors with the particular color...excepting since only the false case will ever actually execute, your code draws the same scatter plot over and over length(index_of_max_data) times, always with the same result.
See the previous Answer for "the MATLAB way"

Sign in to comment.

1 Answer

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
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).


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
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.
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));
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.