Change colour of all scatter points above/below a reference line

1 回表示 (過去 30 日間)
Jigsaw
Jigsaw 2021 年 2 月 15 日
コメント済み: Jigsaw 2021 年 2 月 15 日
My aim is to have a plot that has all scatter points above the diagonal a different colour than the points below the diagonal.
I came across answers for colouring partcular points in the plot that are already known. But In my case I have n number of plots that have random scatter points. The only thing common in all the n plots is the diagonal line and the x,y scale.
figure()
A = [1,3,5,7,2,5,3,7,9,5]; % A and B are examples of one of the n plot data
B = [2,6,3,5,8,6,4,2,7,3];
Diag_X = [0,10]; % Diag_X and Diag_Y form a reference line that is common in all the plot data
Diag_Y = [0,10];
scatter (A, B, '*');
hold on
plot (Diag_X, Diag_Y, 'k');
I'd like a function that could be used on any of the n scatter plots such the diagonal line is taken a reference and all the points above it are red and all below all blue.

採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 15 日
colormap = [1 0 0; 0 0 1]; %red, blue -> above, below
A = [1,3,5,7,2,5,3,7,9,5]; % A and B are examples of one of the n plot data
B = [2,6,3,5,8,6,4,2,7,3];
Diag_X = [0,10]; % Diag_X and Diag_Y form a reference line that is common in all the plot data
Diag_Y = [0,10];
idx = 1 + (A > B);
color = colormap(idx,:);
pointsize = 15;
scatter (A, B, pointsize, color, '*');
hold on
plot (Diag_X, Diag_Y, 'k');
hold off
  1 件のコメント
Jigsaw
Jigsaw 2021 年 2 月 15 日
Works perfectly. Tried it on other plot combinations too. Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by