Plotting 3D bar graph with gradient fill using Matlab

This article explains how to create a 3D bar graph in Matlab, fill each bar with color gradient according to its height and superpose another bar graph with transparent faces.

After following the steps shown below you will be able to generate a plot which looks like:

Step 0: Lets start with a test data consisting of a 4X4 matrix.

>> data = [ 0.9 0 0 -0.8; 0 0.5 0 0; 0 0 0.6 0; -0.85 0 0 0.8];

Step 1: Copy the following code in a Matlab script file and run.

%-------------------------------------------------------------------------%
%-- Get data in matrix form --%
mat = data;
%-- Set default options --%
set(0,'DefaultAxesFontSize', 10, ...
      'DefaultAxesFontName', 'Arial',...
      'DefaultAxesLineWidth', 1.0);
%-- Plot real part --%
figure(1);
b = bar3(mat);
colorbar
zlim([-1 1])    % Set Z-axis range
ax = gca;
ax.FontSize = 10;
ax.XTickLabel = {'\langle00|','\langle01|','\langle10|','\langle11|'};
ax.YTickLabel = {'|00\rangle','|01\rangle','|10\rangle','|11\rangle'};
ax.XTickLabelRotation = -30;
%-------------------------------------------------------------------------%

Here we have first imported the data, set some default settings, performed standard 3d-bar plotting and modified few attributes. The commands are quite self-explanatory. The result of this code snippet is the following graph:

Step 2: In order to obtain gradient fill we need to define its colormap. First, choose the colors for minimum and maximum values of your data in RGB format and the number of color steps for the gradient. Then, use the following code snippet. (Here I have chosen green to blue colormap).

%-------------------------------------------------------------------------%
%-- Set color scheme --%
colormap hsv
% set number of colors
n=100;
% color map with green [0 1 0] and blue [0 0 1] on the edges
C = [1 0 0; 0 0 1]; % red to blue (overridden by next line)
C = [0 1 0; 0 0 1]; % green to blue
% convert to HSV for interpolation
C_HSV = rgb2hsv(C);
% interpolate hue value
C_HSV_interp = interp1([0 n], C_HSV(:, 1), 1:n);
% compose full HSV colormap
C_HSV = [C_HSV_interp(:), repmat(C_HSV(2:3), n, 1)];
% convert back to RGB
C = hsv2rgb(C_HSV);
% set colormap
colormap(C)
for k = 1:length(b)
    b(k).CData = b(k).ZData;
    b(k).FaceColor = 'interp';
end
%-------------------------------------------------------------------------%

The output looks as follows:

Step 3: Here the colormap (as shown in the colorbar) is normalized between the maximum and minimum values of the data. We would like to change it as from -1 to +1. To change the colormap go to Edit -> Colormap of the current figure and this will open a new window as shown below:

Alternatively, one can use the command "colormapeditor" in the matlab command window.

Change "Color data min" to -1 and "Color data max" to +1 (or any other value according to your need). The modified plot will look as follows:

This is what we wanted in the beginning.

Step 4: Now, we will add one more data set to this bar graph. In this case we will use transparent bars instead of a gradient fill. Lets consider the following 4X4 matrix:

>> data_ideal = [ 1 0 0 -0.75; 0 0.5 0 0; 0 0 0.5 0; -0.75 0 0 1];

Then run the following code snippet:

%-------------------------------------------------------------------------%
%-- Add data_ideal --%
mat_idl = data_ideal;
%-- Plot data_ideal with options --%
hold on
b_idl = bar3(mat_idl);
for k = 1:length(b_idl)
    b_idl(k).FaceAlpha = 0;  % Set transparency of face
    b_idl(k).EdgeAlpha = 0.5;  % Set transparency of edge
    %b_idl(k).EdgeColor = 'red';
    %b_idl(k).LineStyle = ':';
    %b_idl(k).LineWidth = 1;
    
    b_idl(k).CData = b(k).ZData;
    b_idl(k).FaceColor = 'interp';
end
hold off
%-------------------------------------------------------------------------%

Here I have commented out few options which one can use. This code will add the data_ideal to the graph but the colorbar will still be normalized to the minimum and maximum data:

Step 5: Finally, one has to repeat step 3 to get the colorbar corrected. This will lead to the final graph as shown in the beginning of this page:

Code: The matlab script used to generate the above plot can be downloaded at the bottom of this page.

Useful Links: