필터 지우기
필터 지우기

Is there a way to make a categorical array of colors?

조회 수: 20 (최근 30일)
Liv
Liv 2022년 7월 11일
댓글: Liv 2022년 7월 12일
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
dpb
dpb 2022년 7월 11일
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.

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

채택된 답변

dpb
dpb 2022년 7월 11일
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
Liv
Liv 2022년 7월 12일
Thank you as well for editing my original question to be more readable. I am making these posts on my phone, so formatting is a little difficult.
Liv
Liv 2022년 7월 12일
Sorry to continue commenting on this- but I wanted to add that my code also includes a toggle variable that toggles some colors in the array on and off depending on checkboxes in a GUI. So the code above will not work by itself. Just wanted to publish my general idea.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by