plot an image with axes that match the source surface plot

조회 수: 3 (최근 30일)
Ian Townend
Ian Townend 2025년 2월 4일
편집: Cris LaPierre 2025년 2월 4일
I am having a problem getting a match between a surface plot and an image of the plot. This is possibly to do with NaNs along border and the problem addressed in https://uk.mathworks.com/matlabcentral/answers/484482-surf-and-nan-plotting-bug. However, I have tried a range of adjustments to the axis without success. Any pointers gratefully appreciated. Some sample code and a grid file:
%load the grid
load('test_grid','-mat'); %load the grid struct
Z = grid.z';
hf = figure('Tag','PlotFig');
ax = axes(hf);
C = pcolor(ax,grid.x,grid.y,Z);
shading interp
axis equal tight
delX = abs(grid.x(2)-grid.x(1));
delY = abs(grid.y(2)-grid.y(1));
xLim = xlim;
yLim = ylim;
%adjusting the x and y limits seems to have no effect on the image plot
yLim(2) = yLim(2)-delY*10;
%overlay a derived image on the original plot
hold(ax,'on')
h_im = imagesc('XData',xLim,'YData',yLim,'CData',C.CData);
h_im.AlphaData = 0.5;
hold(ax,'off')

채택된 답변

Cris LaPierre
Cris LaPierre 2025년 2월 4일
편집: Cris LaPierre 2025년 2월 4일
There is non-uniform spacing in grid.y that is taken into consideration when using pcolor. This is causing the difference in how pcolor renders the image compared to imagesc. You need to determine if this is correct or not.
%load the grid
load('test_grid','-mat'); %load the grid struct
plot(diff(grid.y))
Note that pcolor uses the supplied values of X and Y to display the image while imagesc only used the first and last value and assumes equal spacing between the points.
  • If x has more than two elements, imagesc uses the first and last elements and ignores the other elements.(ref)
The code below shows this. In particular, note the straight vertical line at the top of the center plot.
figure
tiledlayout(1,3)
nexttile
Z = grid.z';
C = pcolor(Z,'EdgeColor','none');
nexttile
C = pcolor(grid.x,grid.y,Z,'EdgeColor','none');
nexttile
imagesc('XData',grid.x,'YData',grid.y,'CData',Z)
axis tight
In short, images cannot have unequal spacing between pixels. So either you cannot use an image to display your data, or you need to update your data to use equal spacing.
Here, if you ignore the grid x and y values, you can get the images to align.
figure
C = pcolor(Z,'EdgeColor','none');
h_im=imagesc('CData',C.CData);
h_im.AlphaData = 0.5;
axis equal tight
Here's another way of overlaying the images using imshowpair.
figure
imshowpair(Z,C.CData)
axis xy equal
  댓글 수: 1
Ian Townend
Ian Townend 2025년 2월 4일
Thank-you for a really comprehensive answer. I am not sure why grid.y is not evenly spaced and will investigate but at least I now know what to test for.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by