Effacer les filtres
Effacer les filtres

How do I get graph colors to show as defined?

7 vues (au cours des 30 derniers jours)
Ted H
Ted H le 12 Jan 2023
Commenté : Cris LaPierre le 13 Jan 2023
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 commentaires
Ted H
Ted H le 12 Jan 2023
Modifié(e) : Ted H le 12 Jan 2023
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 le 12 Jan 2023
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');

Connectez-vous pour commenter.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 12 Jan 2023
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 commentaires
Ted H
Ted H le 13 Jan 2023
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 le 13 Jan 2023
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)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by