plot an image with axes that match the source surface plot

28 ビュー (過去 30 日間)
Ian Townend
Ian Townend 2025 年 2 月 4 日 10:49
編集済み: Cris LaPierre 2025 年 2 月 4 日 19:24
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 日 18:01
編集済み: Cris LaPierre 2025 年 2 月 4 日 19:24
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 日 19:09
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 Center および File ExchangeGraphics Object Properties についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by