How do I fit 2 plots with different axis in 1 frame?

1 view (last 30 days)
I have plotted an image using imagesc command, and its origin is at top left corner, axis starting from (1,1).
I also have some x and y points, that when I use 'plot' function on, gives back an image, with origin at the centre of it/
How do I plot these in a single frame, with the axis being exactly like that in the 1st case.
I just want to shift the data of the 2nd plot, so that it overlaps that in the 1st one.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Jul 2017
The above is not really correct.
When you pass x and y positions to imagesc(), that is the same as setting the XData and YData properties of a call to image(). If the values you pass are scalars then they define the lower left corner and the other pixels are placed one data unit apart. If the values you pass in are scalars, then the first and the last entries define the lower left corner and the upper right corner. Therefore you do not need to linspace() because everything except the first and last will be discarded.
Now, here is the trick: the coordinates you give are the coordinates of the center of the pixels for the corner(s), not the coordinates for the edges. If you had arranged for pixels to correspond to data units and you want the lower left corner to be a data unit (0,0), then the XData YData you should specify should be (1/2, 1/2)
Note: when I talk about bottom left, I am talking about the case where the axes YDir has been set to 'reverse', as would happen with "axis image" having been used. Otherwise the coordinates would refer to the top left and bottom right; often it turns out that when YDir is left 'normal' then you find you need to flipud() the image to get it to show up correctly.
Now, the easiest thing to do in your situation would be to use
imagesc([-2.796866402155208e+06,2.796866334090865e+06], [2.796866368123035e+06, -2.796866436187377e+06], YourImage )
hold on
plot(.....)
hold off
except that you would tweak those values to account for the center-of-pixel addressing that I mentioned above.
If it was for some reason important that the image stay at (1,1) to (5601,5601), such as if you need the data cursor to be able to read off pixel locations more than you need to be able to read off plot locations, then the way to do this would be to use two axes overlapping: use the same Position for the two of them. You might want to take special care with zoom and pan. One way to do that might be to add a listener on PostSet of the XLim or YLim for the "top" axes, and use those properties to compute the appropriate XLim and YLim for the other axes (taking into account that the other axes is using different scaling and limits.)
  2 Comments
Walter Roberson
Walter Roberson on 13 Jul 2017
Are you plotting using imagesc() (or imshow()), or using pcolor(), or using surf(), or using patch with your matrix as a texture map?
"But since the boundary files for that area lies in some other coordinate range"
I do not understand what boundary files you are referring to?
Walter Roberson
Walter Roberson on 14 Jul 2017
Are you using shaperead() from the Mapping toolbox? It should be possible to use that in connection with the other facilities of the Mapping toolbox to create a mapping between the coordinates there and the coordinates from the image; I seem to recall it would involve working with geographic raster reference objects https://www.mathworks.com/help/map/ref/georefpostings.html

Sign in to comment.

More Answers (1)

KSSV
KSSV on 12 Jul 2017
When you use imagesc it plots the given data wrt to it's indices....when your using plot(x,y) these positions are different and imagesc locations are different. So, first you need to get them to the same locations. You may check the below example on how to achieve this:
K = rand(20,20) ;
[nx,ny] = size(K) ;
x = rand(10,1) ;
y = rand(10,1) ;
xi = linspace(min(x),max(x),nx) ;
yi = linspace(min(y),max(y),ny) ;
figure
hold on
imagesc(xi,yi,K) ;
plot(x,y,'*k')
  2 Comments
Aditya Wahi
Aditya Wahi on 13 Jul 2017
Is there some way to make the plot of the size of image, and not the other way round?
KSSV
KSSV on 13 Jul 2017
Not clear....did the answer solve your purpose?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!