フィルターのクリア

Loop with "for" and "if" and then a scatter with different colors

2 ビュー (過去 30 日間)
Rachele Franceschini
Rachele Franceschini 2022 年 2 月 26 日
編集済み: dpb 2022 年 2 月 26 日
I would like one scatter plot with two different colors for different fields.
I have one table with x1, x2, y3.
I make one scatter with scatter(x1,x2,"o","filled"), but I would like that colors were on the base of y3.
Y3 has values from 0 to 81. I would like that fields with values<10 were green, vice versa values>10 were red.

採用された回答

DGM
DGM 2022 年 2 月 26 日
編集済み: DGM 2022 年 2 月 26 日
Consider the example
% placeholder data
x1 = rand(100,1);
x2 = rand(100,1);
y1 = randi([0 81],100,1);
mask = y1<10; % create a mask
% plot the two sets
scatter(x1(mask),x2(mask),50,"og","filled"); hold on
scatter(x1(~mask),x2(~mask),50,"or","filled")
Alternatively, you could use colormapping:
figure
cmap = permute(ind2rgb(mask+1,[1 0 0; 0 1 0]),[1 3 2]);
scatter(x1,x2,50,cmap,"o","filled")

その他の回答 (1 件)

dpb
dpb 2022 年 2 月 26 日
編集済み: dpb 2022 年 2 月 26 日
Alternatively, gscatter with @DGM example...
gscatter(x1,x2,mask,[],[],[],0,'X Variable','Y Variable')
legend('Level 1','Level 2','Location',"northoutside","Orientation",'horizontal')
See the doc for gscatter for all the possible variations of input parameters.

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by