MATLAB Answers

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

11 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

  0 Comments

Sign in to comment.

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.

  2 Comments

Jonathan Schroder
Jonathan Schroder on 13 Jun 2019
Shunichi,
Thank you for this response! I see that this essentially boils down to
imagesc([0.5:9.5], [0.5,9.5],img)
If I type in what you gave me, it works perfectly! However, I want to be able to save this in the property inspector?
What exactly do these need to correlate to in the properties manager? If I set both my Xlim and Ylim to [0.5,9.5], I get the shapes in the proper places, but now the grid placements are all off!
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
imagesc(img);
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.

Sign in to answer this question.

Tags

Products