Different colors for each bar

조회 수: 1 (최근 30일)
Rachele Franceschini
Rachele Franceschini 2022년 3월 24일
댓글: Cris LaPierre 2022년 3월 24일
Hi, I have this problem. I have this data:
x=file.variable %file text (ABR CAM CAL BAS EML...etc)
y=file.variable %number (12 52 32 36 59....etc
z=file.variable %number (2.3 5.4 8.3 1.3....etc)
I would like a bar plot with different colors for each bar (on base of z value) using for example colorbar. Now I have this code, maybe there is some errors within function:
x = file.variable_text;
y = file.variable_number;
z = file.variable_percentage;
[~,arr] = sort(z,'ascend'); %order data
X = reordercats(categorical(x),x(arr));
How I can do?

답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 3월 24일
Perhaps this example from the bar documentation page will help.
b = bar(rand(10,1));
b.FaceColor = 'flat';
b.CData(2,:) = [.5 0 .5];
  댓글 수: 2
Rachele Franceschini
Rachele Franceschini 2022년 3월 24일
But I should change color for each bar (I have 20 bar). But, a way exists to get gradual color changing on base of data (in my case z)?
Cris LaPierre
Cris LaPierre 2022년 3월 24일
My reading of the documentation would indicate this is the recommended approach for setting bar color. There does not appear to be an option to automatically set the color based on value.
See this answer for more.

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

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by