フィルターのクリア

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 Center および File ExchangeDisplay Image についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by