MATLAB: Setting axis properties is cropping the graphic (imagesc)

imagesc

Hello 🙂
I am using imagesc to get a scaled coloured map of a matrix. I also want to decide the possibles values for the x and y axis.
When I try to set them using the following code, I get a graphic that is cropped in half (half of it is white, the other is coloured). Here is the code :
imagesc(matrixA(:,:,1),[0 1]);
set(gca,'XLim',[0 1],'XTick',0:0.1:1)
It is hard for me to tell if the image is cropped or if it is fitted to half the space…
I did try to search on MathWorks answers and on the internet, but to no avail for now. What am I doing wrong? Have you ever had a similar problem? Reading the doc leaves me clueless as to what the problem can be.
Thank you and have a most wonderful day!

Best Answer

When you use imagesc() without specifying XData or YData (or x and y), then the default XData is to place the center of the pixels one data unit apart, so they would be at 1, 2, 3, ... number of columns. When you then set your XLim to [0 1] you are restricting the view on the image to show only what is visible in x range 0 to 1. That is going to be only half of the first column of pixels (with 1 as the center, the first pixel would occupy from 0.5 to 1.5 on the x axis.)
If you want the image to be placed within the range [0 1] then you should instead be using
imagesc(matrixA(:,:,1), [0 1], 'XData', [0 1], 'YData', [0 1]);
However, you might want to adjust that slightly to take into account that those are the centers you are specifying. For example you might want something like
x = linspace(0, 1, size(YourImage,2)+2 );
xlimit = [x(2), x(end-1)];
and then use xlimit as the XData for imagesc