MATLAB: How to calculate how many plot tick would exist if the axis completely filled the figure

imageimage process

Hi,
Thank you in advance for your help.
I'm creating a plot in MATLAB which is then saved and loaded as a background image in other software. The other software then places overlays on top of the background image to create a user output. This other software needs to scale the overlayes to align with the background image, which creates the fairly unique problem I'm struggling with.
Say I have a plot that goes from 0 to 15 in the X axis and 0 to 7 on the Y axis. I need to calculate, if those axis were extended to the full size of the saved figure, what would the axis limits be?
So far I've written code to get the pixel size of the axis and the number of ticks. Then I calculate the number of ticks per pixel, them multiple that by the number of pixles in the figure. This gets me close but not perfect. I'm a little out. So I expect there is something else in the figure rather than just the axis which is taking up pixels.
For example.
If run spy I get this image:
…which is in a figure that's much larger than the axis. If I were to extend the axis to the edge of the figure, then what would be the min and max of each axis?
Its roughly this: (Which i did in paint)
In heindsite spy is perhaps a bad example image as the Y counts down but hopefully it gets my point across.
Thank you!

Best Answer

Given an axis within a figure, compute what the x and y axis limits would be if the axis extended to the edges of the figure.
Demo: See inline comments that explain each step. The variables newXLim and newYLim contain the extended axis limits.
It uses randomly placed axes with randomly set axis limits. After computing the expanded axis limits a second axis is created set to fill the figure and contains the extended axis limits. This axis is used to plot lines that should align with the original axis limits to confirm successful conversion. The axis limits to the exapanded axes are shown in the upper, left corner.
% Create figure with randomly placed axes
% and random data spans (axis limits) to
% test for maximum flexibility
fig = clf();
ax = axes(fig,'Units','Normalize',...
'Position', [rand(1,2)*.4+.05, rand(1,2)*.2+.35], ...
'LineWidth',3);
xlim(ax,sort(rand(1,2)*100))
ylim(ax,sort(rand(1,2)*100))
box(ax,'on')
% Set units to pixels
ax.Units = 'pixels';
fig.Units = 'pixels';
% Get locations and sizes
axPos = ax.InnerPosition; % inner positions!
figPos = fig.Position; % we only need width and height
% Compute margins between fig and ax edges
% in pixels
axMarg = [axPos(1:2),figPos(3:4) - (axPos(1:2)+axPos(3:4))]; %[left, bottom, right, top]
% Now we know how far to extend the axes to each edge
% of the figure in pixel units.
% Convert from pixel units to data units.
xl = xlim(ax);
yl = ylim(ax);
xyFactor(1) = range(xl) / axPos(3);
xyFactor(2) = range(yl) / axPos(4);
xyMargDataUnits = axMarg.*xyFactor([1,2,1,2]);
% Now we know the distance between each axis edge
% and figure edge in data units. We just need to
% extrapolate from the axis limits
newXLim = [xl(1)-xyMargDataUnits(1), xl(2)+xyMargDataUnits(3)];
newYLim = [yl(1)-xyMargDataUnits(2), yl(2)+xyMargDataUnits(4)];
% Set axis units back to normalized so if the figure
% is resized, the results will not change (important!).
ax.Units = 'normalize';
% Now let's test it by adding an underlying axis with these limits
ax2 = axes('Units','Normalize','InnerPosition',[0 0 1 1]);
box(ax2,'on') % added after creating GIF image.

ax2.LineWidth = 1.5; % added after creating GIF image.
uistack(ax2,'bottom')
xlim(ax2, newXLim)
ylim(ax2, newYLim)
% Use reference lines to confirm that the axis limits of
% th original axes align with those values on the expanded axis.
xline(xl(1),'b--', 'LineWidth', 1.5)
xline(xl(2),'b--', 'LineWidth', 1.5)
yline(yl(1),'b--', 'LineWidth',1.5)
yline(yl(2),'b--', 'LineWidth',1.5)
% Show expanded axis limits
text(ax2,min(xlim(ax2)),max(ylim(ax2)),...
sprintf('xlim: [%.1f, %.1f]\nylim: [%.1f, %.1f]', ...
newXLim(1),newXLim(2),newYLim(1),newYLim(2)), ...
'VerticalAlignment','Top','HorizontalAlignment','Left',...
'FontSize', 12, 'FontWeight','bold','fontName','fixedwidth',...
'BackgroundColor', 'w')
The image below shows several iterations of this demo.