How do I change the colour of certain indices in scatterplot?

Hello, I am very new to this.
I am looking to make a scatter plot with some points plot as black point and some as red according to the indices.
I have a 19 elements array and for the values at indices 2, 4, 6, 9, 13, 15, 16 I want the point to be red and the other ones to be black.
Let A be my 19 elemets array
Let X be my X array
Let indice = [2 4 6 9 13 15 16]
I first wrote this:
scatter(X,A,'*k') and now how can I modify this to get the red points at the proper values?
I guess I need to do a if or for but I am not quite sure how do do this. Can anyone help me out?
Thanks

 採用された回答

Adam Danz
Adam Danz 2020 年 9 月 24 日
編集済み: Adam Danz 2020 年 9 月 24 日

0 投票

Here's are 4 demo that all achieve the same result.
Xvals = [1,2,3,4,5,6,7,8,9];
Yvals = [2,5,5,5,2,2,5,5,5];
% Show scatter plot were all y-val greater than 3 are red
% and all y-vals less than or equal to 3 are black.
% METHOD 1
cla()
col = [0 0 0; 1 0 0]; % [black; red]
isGreater = Yvals > 3;
scatter(Xvals, Yvals, 50, col(isGreater+1,:), 'filled')
% METHOD 2 (variation of method 1)
cla()
baseColors = [0 0 0; 1 0 0]; % [black; red]
isGreater = Yvals > 3;
cmap = baseColors(isGreater+1,:);
scatter(Xvals, Yvals, 50, cmap, 'filled')
% METHOD 3
ax = cla();
isGreater = Yvals > 3;
scatter(Xvals, Yvals, 50, isGreater+1, 'filled')
ax.Colormap = [0 0 0; 1 0 0]; % [black; red]
% METHOD 4
cla()
hold on
isGreater = Yvals > 3;
scatter(Xvals(~isGreater), Yvals(~isGreater), 50, 'k', 'filled')
scatter(Xvals(isGreater), Yvals(isGreater), 50, 'r', 'filled')

5 件のコメント

A LL
A LL 2020 年 9 月 25 日
I end up doing something similar to method 4) and it seems to work!
Thanks again for your help!
Here what I wrote:
Where
-A is the array of the values of the x axis
-Value is the array of the values of the y axis
-indice is the array of the indices corresponding to the selected values I want to put in red
figure(1);
hold on;
scatter(A,Value,'*k');
plot(A(indice),Value(indice),'*r');
hold off;
Adam Danz
Adam Danz 2020 年 9 月 25 日
編集済み: Adam Danz 2020 年 9 月 25 日
I see, so you're plotting all of the values in black and then you're plotting a subset of the value on top of the other points in red.
That's a bit suboptimal, especially if you have a lot of data points because your axes must store more data than needed. I also wonder why you use plot() with one set of markers and scatter() with the other.
Assuming indice is a logical vector, why not just do,
plot(A(~indice),Value(~indice),'*k');
plot(A(indice),Value(indice),'*r');
My guess is that indice are subscripts rather than logical indicies. In that case, you can either use the logical indices instead which is recommended, or, if you incist on using subscripts, you could use,
otherIndices = setdiff(1:numel(A), indices);
plot(A(otherIndices),Value(otherIndices),'*k');
plot(A(indice),Value(indice),'*r');
A LL
A LL 2020 年 9 月 25 日
Oh I see! I didn't realised it was suboptimal!
I tried your first option and I don't know why is only shows the red markers...
But the second option works so I just changed my code for this one!
Thanks again
Adam Danz
Adam Danz 2020 年 9 月 25 日
"I didn't realised it was suboptimal"
Yes, logial indexing is faster and maintains the size of the array which is usually helpful.
"I tried your first option and I don't know why is only shows the red markers."
DId you change the threshold? My threshold is at 3 and all of your data are well above 3 so they would all be red if you didn't change the threshold.
"But the second option works so I just changed my code for this one!"
Glad to hear it!
A LL
A LL 2020 年 9 月 25 日
Maybe my last answer wasn't clear but I was refering to the two alternative solutions you proposed to me.
Alternative 1:
plot(A(~indice),Value(~indice),'*k');
plot(A(indice),Value(indice),'*r');
==>This one did not work for me and only plot the red markers... not sure why...
Alternative 2:
otherIndices = setdiff(1:numel(A), indices);
plot(A(otherIndices),Value(otherIndices),'*k');
plot(A(indice),Value(indice),'*r');
==>This one worked and I change my code for this!
Anyway, thanks again!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDiscrete Data Plots についてさらに検索

質問済み:

2020 年 9 月 24 日

コメント済み:

2020 年 9 月 25 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by