Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6355583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:47:39+00:00 2026-05-24T22:47:39+00:00

When laying out a figure in MATLAB, typing axis equal ensures that no matter

  • 0

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

enter image description here

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:

  1. Does anyone know how I can position an axes in a figure by specifying the location relative to another axes?
  2. Assuming I can do 1, how can I have this new axes remain in the same place even if I resize the figure?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:47:40+00:00Added an answer on May 24, 2026 at 10:47 pm

    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 the axis image call, it will be the same:

    figure, plot(1:10,1:10)
    get(gca,'Position')
    pause(1)
    axis image
    get(gca,'Position')
    

    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 AXESRELATIVE for 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:

    %# automatic resize only works for normalized units
    figure
    hParentAx = axes('Units','normalized');
    axis(hParentAx, 'image')
    
    %# create a new axis positioned at normalized units with w.r.t the previous axis
    %# the axis should maintain its relative position on resizing the figure
    [hAx hPan] = axesRelative(hParentAx, ...
        'Units','normalized', 'Position',[0.7 0.1 0.1 0.1]);
    set(hAx, 'Color','r')
    

    And the function implementation:

    function [hAx hPan] = axesRelative(hParentAx, varargin)
        %# create panel exactly on top of parent axis
        s = warning('off', 'MATLAB:hg:ColorSpec_None');
        hPan = uipanel('Parent',get(hParentAx, 'Parent'), ...
            'BorderType','none', 'BackgroundColor','none', ...
            'Units',get(hParentAx,'Units'), 'Position',plotboxpos(hParentAx));
        warning(s)
    
        %# sync panel to always match parent axis position
        addlistener(handle(hParentAx), ...
            {'TightInset' 'Position' 'PlotBoxAspectRatio' 'DataAspectRatio'}, ...
            'PostSet',@(src,ev) set(hPan, 'Position',plotboxpos(hParentAx)) );
    
        %# create new axis under the newly created panel
        hAx = axes('Parent',hPan, varargin{:});
    end
    

    axesRelative


    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:

    %# create fan-shaped coordinates
    [R,PHI] = meshgrid(linspace(1,2,5), linspace(0,pi/2,10));
    X = R.*cos(PHI); Y = R.*sin(PHI);
    X = X(:); Y = Y(:);
    num = numel(X);
    
    %# images at each point (they don't have to be the same)
    img = imread('coins.png');
    img = repmat({img}, [num 1]);
    
    %# plot scatter of images
    SCALE = 0.2;             %# image size along the biggest dimension
    figure
    for i=1:num
        %# compute XData/YData vectors of each image
        [h w] = size(img{i});
        if h>w
            scaleY = SCALE;
            scaleX = SCALE * w/h;
        else
            scaleX = SCALE; 
            scaleY = SCALE * h/w;
        end
        xx = linspace(-scaleX/2, scaleX/2, h) + X(i);
        yy = linspace(-scaleY/2, scaleY/2, w) + Y(i);
    
        %# note: we are using the low-level syntax of the function
        image('XData',xx, 'YData',yy, 'CData',img{i}, 'CDataMapping','scaled')
    end
    axis image, axis ij
    colormap gray, colorbar
    set(gca, 'CLimMode','auto')
    

    image_scatter

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I'm laying out objects on the code, I noticed that I'm having a
When laying out a WinForm in Visual Studio you get the ability to resize
After laying out my schema diagram in SQL Server (both 2000 and 2005), I
Does anyone have any tricks for quickly laying out the asp.net html markup for
I have a question about laying out some swing components. Say, I have a
Does anyone know of a good tool for laying out class diagrams and the
I have been debating using css with div's and laying out elements, and really
What are some edge overlap minimization techniques when laying out a graph? (Preferably related
In PHP , what is the best practice for laying out the responsibilities of
I was developing a web page, where I was laying out a board for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.