Controlling colors in scatter plots

조회 수: 7 (최근 30일)
SM1974
SM1974 2024년 11월 29일
댓글: DGM 2024년 12월 13일
I have an array of label names that have corresponding RGB triplet colors:
% label names
myLabel = ["tomatoes";"carrots";"blueberries";"beans";"bananas"];
myLabelColor = [0.9 0.0 0.0; 1.0 0.7 0.1; 0.4 0.0 0.8; 0.3 0.7 0.0; 0.9 0.9 0.1];
I want to use these custom colors such that the first element of myLabel is always colored with the first element of myLabelColor in my scatter plots and graphs.
Here is some data:
x = rand(10,1);
y = rand(10,1);
groups = ["bananas"; "blueberries"; "bananas"; "beans"; "beans"; "tomatoes";"blueberries";"bananas";"tomatoes";"tomatoes"];
figure;
gscatter(x,y, groups);
Note that the "carrots" label is missing from this data but may appear in another set of data at some point. I don't know which labels will appear in each data set but I know they will always be ones from myLabel array
In the scatter plot, the groups are identified by Matlab's default colors. How can I edit my code so that the groups are identified by my custom colors (bearing in mind that I will want to scale up to larger data sets that may or may not contain all labels)?

채택된 답변

DGM
DGM 2024년 11월 30일
편집: DGM 2024년 11월 30일
This is what I threw together, but there's probably a better way to do this; I haven't really used gscatter() for anything before.
% fake data
x = rand(10,1);
y = rand(10,1);
groups = ["bananas"; "blueberries"; "bananas"; "beans"; "beans"; "tomatoes";"blueberries";"bananas";"tomatoes";"tomatoes"];
% label naming info
myLabel = ["tomatoes";"carrots";"blueberries";"beans";"bananas"];
myLabelColor = [0.9 0.0 0.0; 1.0 0.7 0.1; 0.4 0.0 0.8; 0.3 0.7 0.0; 0.9 0.9 0.1];
% gscatter() will list groups based on order of appearance
% figure out which colors are used and the order they're listed
ugroups = unique(groups,'stable');
% then create a new color table ordered to fit the groups as they appear
[islabeled,idx] = ismember(ugroups,myLabel);
if any(~islabeled) % just to avoid confusion
error('grouping variable contains groups which have no assigned label');
end
CT = myLabelColor(idx,:);
% plot it
hs = gscatter(x,y,groups,CT);
  댓글 수: 4
SM1974
SM1974 2024년 12월 13일
DGM
DGM 2024년 12월 13일
For future readers, the behavior of legend() has been changing and a lot of old answers may be problematic. Walter's comment is the tip of the iceberg. See also Adam's answer here:
Star Strider and I did a bit of a summary review here:
The short story is that many of these answers rely on an undocumented (iirc deprecated) output from legend() which may cause unexpected formatting problems if its requested. The form of the descendant objects that make up the legend items has also changed over the years, so that changes how one would search for them and/or customize them.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Identification에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by