Change bar plot colours in MATLAB 2014b

조회 수: 2 (최근 30일)
Joseph
Joseph 2015년 2월 20일
답변: Joseph 2015년 2월 20일
In MATLAB 2014a and earlier, one could use "get" and "set" to control the colour of the individual bars of a bar plot using the code below.
x=0:10; y=rand(size(x));
figure(1)
b=bar(x,y);
b_child=get(b,'Children');
set(b_child,'CData',x)
%set(b_child,'CData',y)
However, this does not appear to work in MATLAB 2014b; "b.Children" is an empty GraphicsPlaceholder array. Is there a way? I realise that it would be possible to declare "hold on" and plot each bar individually, but I would rather avoid this.

답변 (1개)

Joseph
Joseph 2015년 2월 20일
In case it is useful to anyone, this is the workaround I am using.
x=0:10; y=rand(size(x));
C=colormap;
c=interp1((0:size(C,1)-1)/(size(C,1)-1),C,x./max(x));
figure(1)
barr(x,y,c);
with the function
function barr(x,y,c)
for count=1:max(size(x))
X=ones(size(x))*NaN;
Y=X;
X(count)=x(count);
Y(count)=y(count);
bar(X,Y,'FaceColor',c(count,:))
if count ==1; hold on; end
end
hold off
end

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by