MATLAB: How to set the ZScale of a BAR3 plot to logarithmic in MATLAB

bar3logMATLABzscale

I am setting the Zscale of a BAR3 plot to logarithmic, but I do not get an appropriate BAR3 plot:
Y = cool(7);
bar3(Y)
set(gca,'ZScale','log')

Best Answer

The bar chart is drawn incorrectly when the axes ZScale is changed to a logarithmic scale because the surface objects in the plot may have a lower value of 0 which cannot be rendered on a log scale. To work around this issue, use the following code after calling BAR3 to ensure it is rendered correctly:
Y = cool(7);
bar3(Y)
set(gca,'ZScale','log')
llim = .1;
h = get(gca,'Children');
for i = 1:length(h)
ZData = get(h(i), 'ZData');
ZData(ZData==0) = llim;
set(h(i), 'ZData', ZData);
end