MATLAB: How do you control “bar3d” plots transparency and color

3dbargraphMATLABplot

How can I change the transparency and color of a 3D bar graph?

Best Answer

To explain the issue, I am using the example found here:

https://www.mathworks.com/help/matlab/ref/bar3.html#bt1bonh

The code to create the bar charts is:

% Close existing plots
close all
load count.dat
Z = count(1:10,:);
% Plot bar graph
figure
b = bar3(Z);
title('Semi-Transparent BarGraph')

After creating the plot you can make the plots transparent by modifying the 'FaceAlpha' property of each group of bar charts in the bar3d object:

% For each surface in bar graph - Loop through
% Set FaceAlpha to 0.3
% Face Alpha controls transparency of surface
% 0 is completely transparent while 1 is opaque
for i = 1:length(b)
  b(i).FaceAlpha = '0.3';
end

In addition, here is further information on changing the colors of the each group of bars individually:

https://www.mathworks.com/matlabcentral/answers/5424-how-to-colorize-individual-bar-in-bar3

Alternatively, you can set a different palette using the "colormap" command:

https://www.mathworks.com/help/matlab/ref/colormap.html