How do I get graph colors to show as defined?

조회 수: 10 (최근 30일)
Ted H
Ted H 2023년 1월 12일
댓글: Cris LaPierre 2023년 1월 13일
I have a table with a variable (my_table.my_config) that holds the configuration names of a component. I added a variable to my table to contain a unique color for each configuration. The configuration variable is categorical, hence the following code:
my_table.my_colors = my_table.my_config; %copy the configuration categorical, then change to define corresponding colors
my_table.my_colors = renamecats(my_table.mb_colors, {'red', 'green', 'blue', 'black'});
The swarm chart is
figure;
y = swarmchart( my_table.xvar, my_table.yvar, 10, my_table.my_color, 'filled');
The colors are not red, green, blue, and black. Any idea why not?
categories(my_table.my_colors)
ans =
4×1 cell array
{'red' }
{'green'}
{'blue' }
{'black'}
  댓글 수: 2
Ted H
Ted H 2023년 1월 12일
편집: Ted H 2023년 1월 12일
Note:
I ran the code in the accepted answer of this thread and got the expected colors.
I followed the example in the section titled Display Filled Markers with Varied Color in the matlab documentation for swarmchart.
Ted H
Ted H 2023년 1월 12일
I think I figured it out.
I revised and added as follows:
my_table.my_colors = my_table.my_config; %copy the configuration categorical, then change to define corresponding colors
my_table.my_colors = renamecats(my_table.mb_colors, {'1', '2', '3', '4'});
my_table.my_colors = double(my_table.my_colors);
map = [0 0 1
0 1 0
1 0 0
0 0 0];
and the swarmchart becomes
figure;
colormap(map);
y = swarmchart( my_table.xvar, my_table.yvar, 10, my_table.my_color, 'filled');

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

채택된 답변

Cris LaPierre
Cris LaPierre 2023년 1월 12일
The issue is that color name can only be used to specify a single color for all data points. If you want to specify a different color for each point, you must use a numeric value
  • RGB triplet
  • matrix of RGB triplets
  • vector of colormap indices
(see the table here).
If you are particular about the color used, then I suggest predefining the colors, and using group numbers to select the appropriate color. Here's a simple example
my_table = table(randi(2,100,1),randn(100,1),'VariableNames',["xvar","yvar"]);
my_table.my_config = discretize(my_table.yvar,4);
% define colors
colors = [1 0 0; 0 1 0; 0 0 1; 0 0 0];
% use group number from my_config to select rgb triplet
my_table.my_colors = colors(my_table.my_config,:)
my_table = 100×4 table
xvar yvar my_config my_colors ____ ________ _________ ___________ 2 0.1512 3 0 0 1 2 -0.43599 3 0 0 1 2 1.1765 4 0 0 0 2 0.34772 3 0 0 1 1 -0.26273 3 0 0 1 2 -0.40591 3 0 0 1 2 0.19128 3 0 0 1 2 -0.10313 3 0 0 1 2 1.3358 4 0 0 0 1 0.35625 3 0 0 1 1 0.8239 4 0 0 0 1 -0.98383 2 0 1 0 1 -1.1343 2 0 1 0 2 0.37888 3 0 0 1 1 -0.22596 3 0 0 1 2 1.6522 4 0 0 0
s = swarmchart(my_table,'xvar','yvar','filled','ColorVariable','my_colors','SizeData',50);
  댓글 수: 4
Ted H
Ted H 2023년 1월 13일
Can you confirm if the SizeData pair cannot be before the 'filled' entry? I thought I tried that pair, and it bombed.
Cris LaPierre
Cris LaPierre 2023년 1월 13일
You are using the table syntax, so you must follow that convention:
swarmchart(mytable,"xvar","yvar",'filled',...)
If you want to use the array syntax, you would have to extract vectors from the table.
swarmchart(mytable.x,mytable.y,size,mytable.my_color)

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by