필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Gaetan
Gaetan 2023년 12월 12일
답변: Voss 2023년 12월 12일
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!

채택된 답변

Walter Roberson
Walter Roberson 2023년 12월 12일
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)

추가 답변 (1개)

Voss
Voss 2023년 12월 12일
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')

카테고리

Help CenterFile Exchange에서 Display Image에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by