- If x has more than two elements, imagesc uses the first and last elements and ignores the other elements.(ref)
plot an image with axes that match the source surface plot
조회 수: 3 (최근 30일)
이전 댓글 표시
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')
댓글 수: 0
채택된 답변
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.
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
figure
imshowpair(Z,C.CData)
axis xy equal
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!