How to use colormap for different bars.

조회 수: 18 (최근 30일)
ref
ref 2011년 11월 10일
I am trying to plot a bar chart with error bars, giving different colors to the individual bars using colormap function, but instead I get the same color for all bars. How can i set the bars following one specific colormap?
y = [4.2; 4.6; 5]; %The data.
s = [.3; .2; .6]; %The standard deviation.
h = bar(y);
colormap(hsv(h))
hold on
set(gca, ...
'XTickLabelMode', 'manual', ...
'XTickLabel', {'R0', 'R1', 'R2'})
h1 = errorbar(y,s,'r');
set(h1,'linestyle','none')

채택된 답변

Jonathan
Jonathan 2011년 11월 11일
See if this helps.
y = [4.2; 4.6; 5]; %The data.
s = [.3; .2; .6]; %The standard deviation.
fHand = figure;
aHand = axes('parent', fHand);
hold(aHand, 'on')
colors = hsv(numel(y));
for i = 1:numel(y)
bar(i, y(i), 'parent', aHand, 'facecolor', colors(i,:));
end
set(gca, 'XTick', 1:numel(y), 'XTickLabel', {'R0', 'R1', 'R2'})
errorbar(y,s,'r');
  댓글 수: 1
ref
ref 2011년 11월 11일
Thank you, exactly what i was looking for.

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

추가 답변 (1개)

ref
ref 2011년 11월 11일
Sorry, I have one more question. When "y" has 3 elements, then the colors I get for the bars are "red, green & blue". When I change the elements of "y" to 4, or 5, Matlab does not give the same colors. Instead it gives "red, light green, light blue and purple". Is it possible to hold constant the colors for the elements? I need the same color for the first-second-third element independently the number of elements of "y".
  댓글 수: 2
Jonathan
Jonathan 2011년 11월 11일
You can try something like this modification.
bar(1, y(1), 'parent', aHand, 'facecolor', 'r');
bar(2, y(2), 'parent', aHand, 'facecolor', 'g');
bar(3, y(3), 'parent', aHand, 'facecolor', 'b');
colors = copper(numel(y) - 3);
for i = 4:numel(y)
bar(i, y(i), 'parent', aHand, 'facecolor', colors(i-3,:));
end
ref
ref 2011년 11월 11일
Thx a lot Jonathan.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by