MATLAB Answers

Grid in imagesc() not lining up to grid limits?

32 views (last 30 days)
Jonathan Schroder
Jonathan Schroder on 13 Jun 2019
Commented: Shunichi Kusano on 13 Jun 2019
Hey everyone,
I'm trying to figure out why my plotted matrix here, a 10 x 10 matrix with mostly 0s, represented in deep blue, is not lining up with my axes of this graph! I have tried playing with the Xlim/YLim as well as XTick/YTick to no avail. It's driving me nuts - I want to be able to represent this data in a clear and concise way.
That yellow block you see is supposed to be 5 units long, and the orange block is supposed to be 4 units long. Something is amiss here, but I am not equipped to solve it with my developing knowledge of plotting figures.
Is my plotting tool the wrong one to use? Am I missing a crucial setting for this? I will be happy to provide more information.
Thank you for your help!
hey l6SscBU.png

Answers (1)

Shunichi Kusano
Shunichi Kusano on 13 Jun 2019
Hi Jonathan. The pixel coordinate is normal in your image. The center of each pixel is located at the cross of the grid lines. Now, you need to specify the coordinate of your image like:
imagesc([1:size(img,2)]-0.5, [1:size(img,1)]-0.5, img); % img is your image
This makes your image lined up with your grid. hope this helps.
Shunichi Kusano
Shunichi Kusano on 13 Jun 2019
I may misunderstand what you want, though, how about this? I just added lines manually.
img = rand(10,10); % dummy data
line(repmat([1.5:1:9.5],2,1),repmat([0;11], 1, 9), 'Color', 'red'); % vertical
line(repmat([0;11], 1, 9), repmat([1.5:1:9.5],2,1), 'Color', 'red'); % horizontal
ax = gca;
ax.TickLength = [0 0];

Sign in to comment.



Community Treasure Hunt

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

Start Hunting!