When laying out a figure in MATLAB, typing axis equal ensures that no matter what the figure dimensions, the axes will always be square:

My current problem is that I want to add a second axes to this plot. Usually, that’s no problem; I would just type axes([x1 y1 x2 y2]), and a new square figure would be added with corners at (x1, y1), (x2, y2), which is a fixed location relative to the figure. The problem is, I want this new axes to be located at a fixed location relative to the first axes.
So, my questions are:
- Does anyone know how I can position an axes in a figure by specifying the location relative to another axes?
- Assuming I can do 1, how can I have this new axes remain in the same place even if I resize the figure?
An axis position property is relative to its parent container. Therefore, one possibility is to create a transparent panel with the same size as the first axis, then inside it create the second axis, and set its location and size as needed. The position specified would be as if it were relative to the first axis.
Now we need to always maintain the panel to be the same size/location as the first axis. Usually this can be done using LINKPROP which links a property of multiple graphic objects (panel and axis) to be the same, namely the
'Position'property.However, this would fail in your case: when calling
axis image, it fixes the data units to be the same in every direction by setting aspect ratio properties like'PlotBoxAspectRatio'and'DataAspectRatio'. The sad news is that the'Position'property will not reflect the change in size, thus breaking the above solution. Here is an example to illustrate the problem: if you query the position property before/after issuing theaxis imagecall, it will be the same:Fortunately for us, there is a submission on FEX (plotboxpos) that solves this exact issue, and returns the actual position of the plotting region of the axis. Once we have that, it’s a matter of syncing the panel position to the axis position. One trick is to create a event listener for when the axis changes size (it appears that the
'TightInset'property changes unlike the'Position'property, so that could be the trigger in our case).I wrapped the above in a function
AXESRELATIVEfor convenience: you call it as you would the builtin AXES function. The only difference is you give it as first argument the handle to the axis you want to relatively-position the newly created axis against. It returns handles to both the new axis and its containing panel.Here is an example usage:
And the function implementation:
On a completely different note: before you recent edit, I got the impression that you were trying to produce a scatter plot of images (i.e like a usual scatter plot, but with full images instead of points).
What you suggested (from what I understand) is creating one axis for each image, and setting its position corresponding to the x/y coordinates of the point.
My solution is to use the IMAGE/IMAGESC functions and draw the small images by explicitly setting the
'XData'and'YData'properties to shift and scale the images appropriately. The beauty of this is it require a single axis, and doesn’t suffer from having to deal with resizing issues..Here is a sample implementation for that: