Changing bar graph bar colors

조회 수: 2 (최근 30일)
Hail
Hail 2014년 7월 24일
편집: Kelly Kearney 2014년 7월 24일
I have a bar graph that creates 4 different bars. I want the first bar to be colored black, the second blue, the third green, and the fourth red. How do I achieve this?
Thanks

채택된 답변

Kelly Kearney
Kelly Kearney 2014년 7월 24일
편집: Kelly Kearney 2014년 7월 24일
By default, all the bars in a bar graph are part of the same patch object, and therefore are the same color. To get different colors, you need to plot each bar as its own object.
x = 1:4;
y = rand(1,4);
h = bar(x, diag(y), length(y));
set(h, {'facecolor'}, {'k','b','g','r'}');
or
h = bar(x, diag(y), 'stacked');
set(h, {'facecolor'}, {'k','b','g','r'}');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by