Clear Filters
Clear Filters

How to change color of point depending on side of a line?

3 views (last 30 days)
Hello,
I am producing a scatter plot using two columns of data from a .db file and would like to change the color of each of the data points based off their location relative to two lines in the figure. That is, if:
1) the point is to the left of some xline, that point should be some color
2) the point is to the right of the xline AND above some yline, let the point be another color
3) the point is to the right of the xline and below some yline, have it be yet another color
I don't think I can include scatter in a for loop and don't know if I should organize each of the points in the table data into separate "bins" to then scatter individually (don't know how to do that either, to be honest).
Any and all help would be greatly appreciated. Thank you!

Accepted Answer

Walter Roberson
Walter Roberson on 12 Dec 2023
You can include scatter in a for loop.
But there is a better way.
state = ones(NumberOfPoints,1);
state(X > AppropriateXValue & Y < AppropriateYValue) = 2;
state(X > AppropriateXValue & Y >= AppropriateYValue) = 3;
cmap = [
0 0 0 %first color
0 0 1 %second color
1 0 0 %third color
];
scatter(X, Y, [], state)
colormap(cmap)

More Answers (1)

Voss
Voss on 12 Dec 2023
x = rand(20,1);
y = rand(20,1);
xv = 0.4;
yv = 0.6;
colors = [1 0 0; 0 1 0; 0 0 1];
r_idx = x < xv;
g_idx = x >= xv & y > yv;
b_idx = x >= xv & y <= yv;
c_idx = zeros(numel(x),1);
c_idx(r_idx) = 1;
c_idx(g_idx) = 2;
c_idx(b_idx) = 3;
c = colors(c_idx,:);
scatter(x,y,[],c)
line([xv xv],[0 1],'Color','k')
line([xv 1],[yv yv],'Color','k')

Categories

Find more on Display Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!