Following my comment, here is a solution. To sum up simply, you cannot change the color of only one bar drawn by bar3
.
Instead of displaying each bar by hand, my solution is to modify the code of bar3
in order to draw each bar independently, which you can read freely. This is rather simple when you analyse the code of bar3
: each bar is the graphic representation of 6*4 data matrices. The block of code in question is the following:
for i=1:size(yy,2)/4
h = [h,surface('xdata',xx+x(i),...
'ydata',yy(:,(i-1)*4+(1:4)), ...
'zdata',zz(:,(i-1)*4+(1:4)),...
'cdata',i*cc, ...
'FaceColor',facec,...
'EdgeColor',edgec,...
'tag','bar3',...
'parent',cax)];
end
As you see, surface
is called on every data column. To call surface
on each element, you can modify the code as follows:
for i=1:size(yy,2)/4
for j=1:size(yy,1)/6
h = [h,surface('xdata',xx((j-1)*6+(1:5),:)+x(i),...
'ydata',yy((j-1)*6+(1:5),(i-1)*4+(1:4)), ...
'zdata',zz((j-1)*6+(1:5),(i-1)*4+(1:4)),...
'cdata',i*cc((j-1)*6+(1:5),:), ...
'FaceColor',facec,...
'EdgeColor',edgec,...
'tag','bar3',...
'parent',cax)];
end
end
You cannot modify the original bar3
, so let's save it as bar3_mod
.
With this done, if you refer to the doc article about Color 3-D Bars by Height, it is now really simple to make bars of zero height transparent. Before this, remember that the height of a bar you get with a get
on the handle of one bar is described by a 5*4 matrix of the form:
NaN 0 0 NaN
0 Z Z 0
0 Z Z 0
NaN 0 0 NaN
NaN 0 0 NaN
So you must only test the value of the element at (2,2) and change the color as you want. In your case, it's quite simple to derive the code given in the linked page:
h = bar3_mod(Z);
for k = 1:length(h)
zdata = get(h(k),'ZData');
if zdata(2,2)==0
set(h(k),'CData',zdata,'FaceColor','none');
end
end
I've tested it on an example, with magic(5)
as input and making the bar with a height of 1 transparent:

EDIT
Like bar3
, there is one color per data column. If you want to color each bar according to it's value, you can either modify the code of bar3_mod
or add a few more instructions when you make the specified bars transparent.
1st solution: quite simple to change the for-loop:
for i=1:size(yy,2)/4
for j=1:size(yy,1)/6
h = [h,surface('xdata',xx((j-1)*6+(1:5),:)+x(i),...
'ydata',yy((j-1)*6+(1:5),(i-1)*4+(1:4)), ...
'zdata',zz((j-1)*6+(1:5),(i-1)*4+(1:4)),...
'cdata',zz((j-1)*6+2,(i-1)*4+2)*cc((j-1)*6+(1:5),:), ... % here is the modification
'FaceColor',facec,...
'EdgeColor',edgec,...
'tag','bar3',...
'parent',cax)];
end
end
2nd solution: just add a else
case where you affect the new color data:
for k = 1:length(h)
zdata = get(h(k),'ZData');
if zdata(2,2)==0
set(h(k),'CData',zdata,'FaceColor','none');
else
set(h(k),'CData',ones(size(zdata))*zdata(2,2));
end
end