Show an image in a MATLAB 3D surface plot with a separate colormap

The surface / surf plot in MATLAB can visualize data in 3D. When I took a course in grad school on statistical image processing, I saw a very interesting plot where data is shown as a surf plot and underneath, on the ground or x-y plane, an image is shown. The pixels of the image corresponded to the points in the 3D surface and gave some extra information about the each point, sort of like an image-based version of surfc or a 3D version of plotting over an image background. I always wanted to know how to make that plot but rather than asking the prof who made it (as one is supposed to after paying tuition), I decided to figure it out on my own (thus proving why I was never good at accounting). It took some experimentation but I finally figured out how this type of plot is accomplished. In this tutorial, I will show how to do this and how to make it so that the surface plot and the image can use different colormaps, getting around the restriction that MATLAB only has one colormap per figure. In effect, this will simulate multiple colormaps. The result will look something like this:

Image shown as a texture mapped plane in a MATLAB 3D surface plot

Note how the image is mapped to a plane and is shown with a jet colormap while the 3d surf is shown with a gray colormap.

Basic way to show an image on the same axes as a 3D surface

I will first go through the basic way to do this without multiple colormap support. Take a look at this code:

% the data that you want to plot as a 3D surface.
[x,y,z] = peaks;
 
% get the corners of the domain in which the data occurs.
min_x = min(min(x));
min_y = min(min(y));
max_x = max(max(x));
max_y = max(max(y));
 
% the image data you want to show as a plane.
planeimg = abs(z);
 
% set hold on so we can show multiple plots / surfs in the figure.
figure; hold on;
 
% do a normal surface plot.
surf(x,y,z);
 
% set a colormap (but this has no effect because the next colormap
% command overwrites it)
colormap(gray);
 
% desired z position of the image plane.
imgzposition = -10;
 
% plot the image plane using surf.
surf([min_x max_x],[min_y max_y],repmat(imgzposition, [2 2]),...
    planeimg,'facecolor','texture')
 
% set a colormap for the figure.
colormap(jet);
 
% set the view angle.
view(45,30);
 
% labels
 
xlabel('x');
ylabel('y');
zlabel('z');

The above code will produce this plot:

Image shown as a texture mapped plane in a MATLAB 3D surface plot

I first created the 3D data to be plotted by making use of the peaks function. Next, I found the domain of the data (extent in the x-y plane). This is used to define the plane that the image will be mapped to in the final plot. The image to be shown is stored in planeimg (in this case I simply used abs(z) but the image can any arbitrarily sized array of data - it will be stretched or squeezed to fit the plane defined by the extent of the domain of the data). I then plotted the 3D data by calling surf. Finally, the image is mapped to a plane parallel to the x-y plane by the following lines:

% desired z position of the image plane.
imgzposition = -10;
 
% plot the image plane using surf.
surf([min_x max_x],[min_y max_y],repmat(imgzposition, [2 2]),...
    planeimg,'facecolor','texture')
 

imgzposition sets where on the z axis the image plane is to be placed; I set -10 so it would not intersect with the existing 3D surface. The surf command here has defined a planar surface with the following vertices:

(min_x, min_y, imgzposition)

(max_x, min_y, imgzposition)

(max_x, max_y, imgzposition)

(min_x, max_y, imgzposition)

To understand how this works, refer to MATLAB's surf documentation, which states:

surf(X,Y,Z) creates a shaded surface using Z for the color data as well as surface height. X and Y are vectors or matrices defining the x and y components of a surface. If X and Y are vectors, length(X) = n and length(Y) = m, where [m,n] = size(Z). In this case, the vertices of the surface faces are (X(j), Y(i), Z(i,j)) triples.

In any case, the planeimg is used as a texture map for the single face of the planar surface.

You will have noticed that I actually have two colormap commands in the code, but both the 3D surface and the image have the same colormap. This is because there is only one colormap for the figure. The next section will discuss how to get around this and use a different colormap for the 3D surface and the image plane.

Show the image with a different colormap by faking multiple colormaps

To give the image a different colormap than the 3D surface, all I need to do is convert the image (which is just a 2D array of values) into a true color image according to the desired colormap. This is how:

% scale the between [0, 255] in order to use a custom color map for it.
minplaneimg = min(min(planeimg)); % find minimum first.
scaledimg = (floor(((planeimg - minplaneimg) ./ ...
    (max(max(planeimg)) - minplaneimg)) * 255)); % perform scaling
 
% convert the image to a true color image with the jet colormap.
colorimg = ind2rgb(scaledimg,jet(256));

Due to the way colormap and ind2rgb work, the image must first be scaled to between [0, 255] before being converted to true color. I also have to specify a 256 element colormap (since [0, 255] has 256 possible values).

Now all we have to do is instead of showing planeimg, we show colorimg instead:

% plot the image plane using surf.
surf([min_x max_x],[min_y max_y],repmat(imgzposition, [2 2]),...
    colorimg,'facecolor','texture')

This will produce a figure very similar to the one at the beginning of this tutorial. If you want to get rid of the wireframe mesh (as I did), then simply specify 'edgecolor' to be 'none' in your surf commands:

surf(x,y,z,'edgecolor','none');

Complete code snippet

Here it is altogether:

% the data that you want to plot as a 3D surface.
[x,y,z] = peaks;
 
% get the corners of the domain in which the data occurs.
min_x = min(min(x));
min_y = min(min(y));
max_x = max(max(x));
max_y = max(max(y));
 
% the image data you want to show as a plane.
planeimg = abs(z);
 
% scale image between [0, 255] in order to use a custom color map for it.
minplaneimg = min(min(planeimg)); % find the minimum
scaledimg = (floor(((planeimg - minplaneimg) ./ ...
    (max(max(planeimg)) - minplaneimg)) * 255)); % perform scaling
 
% convert the image to a true color image with the jet colormap.
colorimg = ind2rgb(scaledimg,jet(256));
 
% set hold on so we can show multiple plots / surfs in the figure.
figure; hold on;
 
% do a normal surface plot.
surf(x,y,z,'edgecolor','none');
 
% set a colormap for the surface
colormap(gray);
 
% desired z position of the image plane.
imgzposition = -10;
 
% plot the image plane using surf.
surf([min_x max_x],[min_y max_y],repmat(imgzposition, [2 2]),...
    colorimg,'facecolor','texture')
 
% set the view.
view(45,30);
 
% label the axes
xlabel('x');
ylabel('y');
zlabel('z');

This will produce the figure shown at the beginning of the tutorial:

Image shown as a texture mapped plane in a MATLAB 3D surface plot

About Peter Yu I am a research and development professional with expertise in the areas of image processing, remote sensing and computer vision. I received BASc and MASc degrees in Systems Design Engineering at the University of Waterloo. My working experience covers industries ranging from district energy to medical imaging to cinematic visual effects. I like to dabble in 3D artwork, I enjoy cycling recreationally and I am interested in sustainable technology. More about me...

Feel free to contact me with any questions about this site at [user]@[host] where [user]=web and [host]=peteryu.ca

Copyright © 1997 - 2021 Peter Yu