# How to overlay a surf plot with a 2D matrix

조회 수: 5 (최근 30일)
Yoni Verhaegen -WE-1718- 2022년 6월 25일
답변: Infinite_king 2023년 10월 2일
Hi all,
I have a 3D matrix (300x178x125) describing the surface of a glacier, which is plotted with the slice command:
clear all
length = 50;
c1 = [204, 255, 255]/255;
c2 = [130, 200, 255]/255;
colors_p = [linspace(c1(1),c2(1),length)', linspace(c1(2),c2(2),length)', linspace(c1(3),c2(3),length)'];
glacier = double(squeeze(glacier));
h = slice(realx,realy,realz,glacier, [], [], 1:size(glacier,1));
set(h, 'EdgeColor','none', 'FaceColor','interp', 'FaceLighting','gouraud')
colormap(colors_p)
colorbar;
xlim([1000 3000])
ylim([0 6000])
zlim([0 125])
caxis([0 40])
daspect([0.5 1 0.1])
view(-142,31)
set(gcf, 'color', 'white')
which then looks like this:
I now want to overlay a 2D matrix (300x178) on the surface of the glacier:
How can I overlay the 2D matrix on top of the glacier surface with 0 put to transparant?
##### 댓글 수: 2없음 표시없음 숨기기
It sounds like you want to apply the color mapping of the 2D image to the glacier surface object. Is that correct?
You could use texture mapping; warp might be useful; or it might be as easy as applying CData to the glacier object. Tough to tell without more info.
Yoni Verhaegen -WE-1718- 2022년 6월 25일
@Adam Danz Yes that is correct! I added the corresponding matrices in attachment in case this would make my question clearer. The matrix h_deb is what I would like to show on top of the glacier surface.

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Infinite_king 2023년 10월 2일
Hi,
I understand that you want to create a surface plot using the provided 3D matrix 'glacier' and overlay color information from the 2D matrix 'h_deb'.
The 'glacier' matrix contains volume data, so we need to first extract the surface information before we can plot it. To obtain the height information of the surface for each point in the X-Y grid, you can refer the below code snippet.
% for every point (i,j) in x,y grid, we are ..
% finding the index 'k', at which we see first see 'nan', ..
% which means the height of surface at [realx(i), realy(j)] is realz(k)
for i = 1 :300
for j = 1:178
% For every point in x,y grid
height(i,j) = nan; % initializing the height as 'nan'
for k = 1:125
% height of the surface at (i,j) index
if isnan(glacier(i,j,k)) == false
height(i,j) = realz(k);
break;
end
end
end
end
Using this surface information we can plot the surface using ‘surf’ function. The colour information can be passed while plotting the surface. Please refer the below code snippet,
% Create a new figure
figure;
% Plot the surface with the given color information from 'h_deb'
surf(realx, realy, height, h_deb);
For more information on ‘surf’ and ‘slice’ function, refer the following documentation,
For more information on colour maps and how to create custom colour maps, refer the following documentation,
1. https://www.mathworks.com/help/matlab/ref/colormap.html

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by