Adjust plot to image size

Hi Everyone,
I want to plot a graph over an image. the image is 750X800 but my graph goes from 0 to 20 on x axis and 0 to 36 on y axis. As a result the graph looks very small and shrinked up. How can I adjust the size of graph so it looks normal over the image.

 Accepted Answer

If you use image() or imagesc() to draw the image, then you can specify leading parameters that are boundaries of where to place the image in data coordinates.
image(xlocation, ylocation, ImageArray)
If you use image() or imagesc() or imshow() then you can instead pass in 'XData' and 'YData' options to control the positions:
image(ImageArray, 'XData', xlocation, 'YData', ylocation)
The x location you pass in is a vector of values. The first value is used to control the x data coordinate at which the center of the bottom left pixel is to be placed. The last value is used to control the x data coordinate at which the center of the top right pixel is to be placed. Any other entries in the vector are ignored. Likewise, the y vector is used to control the y data coordinate of the center of bottom left, and center of top right.
I emphasize: coordinates of centers . So if you give a data coordinate of 0 then do not be surprised to see the last pixel sticking out past 0.

4 Comments

Hi Walter,
Thanks for the answer but my issue is the plot I'm trying to draw over the image. I don't want to change anything in the image but rather adjust the plot size to match the image height and width. I've tried the 'position' function but that doesn't help.
In that case, you will have to use multiple axes, such as yyaxis
I don't want to change anything in the image
The difference between specifying data coordinates for the image, versus using "natural" data coordinates of it and arranging the plot to cover the same space on the axes, comes down to which coordinate system you need the datacursor to indicate.
Is it really the case that if the user activates the data cursor, that you want the coordinates they get back to be the pixel row / column numbers, disconnected from the x and y values of the plot you are drawing?
img = sort(randi([0 255], 240, 320, 3, 'uint8'));
x = 0:20;
y = rand(1,length(x)) * 36;
fig1 = figure(1);
ax1 = axes('Parent', fig1);
image(ax1, [0 20], [0 36], img);
ax1.YDir = 'normal';
hold(ax1, 'on');
plot(ax1, x, y, 'r');
xlim(ax1, [0 20]); ylim(ax1, [0 36]);
hold(ax1, 'off');
fig2 = figure(2);
ax2left = axes('Parent', fig2);
image(ax2left, img);
ax2left.YDir = 'normal';
xlim(ax2left, [0 320]); ylim(ax2left, [0 240]);
ax2left_pos = ax2left.Position;
ax2right = axes('Position',ax2left_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
hold(ax2right, 'on'); %to preserve color
plot(ax2right, x, y, 'r');
xlim(ax2right, [0 20]); ylim(ax2right, [0 36]);
hold(ax2right, 'off')
Thanks Walter. This helps a lot.
Notice the slight border shift, most noticeable at the bottom. If i recall correctly, the default for image() is to put the center of the bottom left pixel at (1,1)

Sign in to comment.

More Answers (0)

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!