Is there a way to make a categorical array of colors?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
b=bar(X,Y,’FaceColor’,ColorArray);
ColorArray={‘1 0 0’,’0 1 0’,’0 0 1’};
Error: Invalid RGB triplet.
Is there a way to assign colors to variables like this? The only other way I know how to assign colors to specific values on a bar graph is by using CData.
b.FaceColor=‘flat’;
b.CData(1,:)=[1 0 0];
b.CData(2,:)=[0 1 0];
b.CData(3,:)=[0 0 1];
I would like the colors to be in a categorical array that way I can assign them to specific variables rather than the order that they appear in the bar graph. Is there any way to do this?
1 comentario
dpb
el 11 de Jul. de 2022
The first doesn't create the ColorArray until after try to use it -- but an RGB triplet is numeric, not character string, anyways.
The 'FaceColor' property is a scalar -- it is either 'flat' if color is to be set via the CData property or one of a single RGB triplet, hex color code, or color name/short name. That's it -- no other choices are available, like setting individual bar colors in that fashion.
It's an awful state of affairs; I've railed about the bar user interface for 30+ years but gotten almost nowhere. It (bar, that is) needs a complete new remake; the UI and internal design was horribly misguided from the beginning.
Respuesta aceptada
dpb
el 11 de Jul. de 2022
See above for rants/railings and a little background from my perspective...more directly to the Q? here --
You can't create a categorical variable that returns an arbitrary RGB triplet; you could use the hex string format or the names/short names style.
But, it's still not terribly convenient as bar simply won't accept a categorical variable -- it's a string or RBG triplet or nothing.
One feature that can make the CData route somewhat less painful is the multiple-valued version of set --
ColorArray=[1 0 0;0 1 0;0 0 1]; % an arbitrary set of color RGB triplets
hB=bar(randi(10,[3 1]),'FaceColor','flat');
set(hB,{'CData'},{ColorArray})
does the deed to set all three bars in the one call -- it can be confusing passing multiple values; see the set documentation for all the details and examples of complex cases similar to the above.
NB: You can still create a named variable that is the index into the color array by which to reference it; note as above it is still a 2D array so have to reference the whole row, not just the row number to use it.
You can store and pass the defined names for predefined colors in the color map as a cellstr array, but you cannot extend that to other arbitrary names/colors pairs; that's reserved for the hex string variant.
3 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Distribution Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!