Multiple colormap in one figure
조회 수: 4 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Vedant Shah
2025년 6월 27일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Color and Styling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!