Multiple colormap in one figure
4 views (last 30 days)
Show older comments
Hi,
I am trying to plot two 3D surface plot with different colormap in one figure.
Do not really know why the freezeColors is not working for me.
Here is my code I took the freezecolor off:
clear all
clc
close all
% Data reading from Excel - 1
x1 = xlsread('temp.xlsx', 'Sheet1', 'B:B');
y1 = xlsread('temp.xlsx', 'Sheet1', 'D:D');
z1 = xlsread('temp.xlsx', 'Sheet1', 'E:E');
xv1 = linspace(min(x1), max(x1), 50);
yv1 = linspace(min(y1), max(y1), 145);
[XX_1,YY_1] = meshgrid(xv1, yv1);
ZZ_1 = griddata(x1,y1,z1,XX_1,YY_1);
% Plotting
figure
plot1=mesh(XX_1,YY_1,ZZ_1);
colormap(jet)
shading interp
set(plot1,'FaceAlpha',0.5);
axis equal
%-----------------------------------------------------------------------------------------------
% Data reading from Excel - 2
x2 = xlsread('temp.xlsx', 'Sheet1', 'I:I');
y2 = xlsread('temp.xlsx', 'Sheet1', 'J:J');
z2 = xlsread('temp.xlsx', 'Sheet1', 'K:K');
xv2 = linspace(min(x2), max(x2), 50);
yv2 = linspace(min(y2), max(y2), 145);
[XX_2,YY_2] = meshgrid(xv2, yv2);
ZZ_2 = griddata(x2,y2,z2,XX_2,YY_2);
% Plotting
hold on
plot2=surf(XX_2,YY_2,ZZ_2);
colormap(hot)
shading interp
set(plot2,'FaceAlpha',0.8);
%-----------------------------------------------------------------------------------------------
% Polish graph
hold off
title('Interface Temp')
xlabel('Y (mm)')
ylabel('X (mm)')
zlabel('Temperature (^{\circ}C)')
colorbar
view(45,45)
grid off
Thanks.

0 Comments
Answers (1)
Vedant Shah
on 27 Jun 2025
A single axes object in MATLAB can only have one colormap at a time. When ‘colormap(jet)’ is called followed by ‘colormap(hot)’, the second call overwrites the first, resulting in both surfaces using the last colormap applied.
One possible solution is to overlay two axes in the same figure, assigning a different colormap to each, and setting their backgrounds to be transparent. This approach allows each surface to retain its respective colormap. The following code snippet demonstrates this method:
% Plotting
figure
set(gcf, 'Color', 'w')
% First axes
ax1 = axes;
mesh1 = mesh(XX_1, YY_1, ZZ_1);
colormap(ax1, jet)
shading interp
set(mesh1, 'FaceAlpha', 0.5)
axis equal
hold(ax1, 'on')
% Second axes, placed in the same position
ax2 = axes;
mesh2 = surf(XX_2, YY_2, ZZ_2);
colormap(ax2, hot)
shading interp
set(mesh2, 'FaceAlpha', 0.8)
% Overlay axes: make both transparent
set(ax1, 'Color', 'none')
set(ax2, 'Color', 'none', ...
'XTick', [], 'YTick', [], 'ZTick', [], ...
'XColor', 'none', 'YColor', 'none', 'ZColor', 'none')
linkaxes([ax1, ax2])
Using this approach with sample data, each surface can be visualized with its own colormap, resulting in a figure as shown below:
For further information, refer to the following documentations:
0 Comments
See Also
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!