MATLAB Answers


How to get the size or position of the plot box within a figure window?

Asked by paul harder on 6 Mar 2014
Latest activity Commented on by Gert Kruger on 7 Nov 2018
After much effort, I am still struggling to find the command to get the size or position of the plot box (i.e. the white box) within a figure window. What I really need is the aspect ratio of this white box, but a size or position command would get me there. I've tried variations of 'Position', 'PlotBoxAspectRatio', 'pbaspect',and 'DataAspectRatio' without luck.
For example, if I run the following command,
I want to know about the physical size/shape of the white box ONLY, not the grey box:

  1 Comment

and how to get the size of the grey area? Because as far as I know, figure size consist the whole are with title bar also. But I need only the grey 'drawable' area.

Sign in to comment.

2 Answers

Answer by Dishant Arora on 6 Mar 2014
Edited by Dishant Arora on 6 Mar 2014
 Accepted Answer

h =figure;
axesHandles = findall(h,'type','axes');


Show 1 older comment
Go through this documentation for axes position property:
By making axis SQUARE you disable stretch to fill , the axes are made as large as they can be under the constraint that each axes has the same length without extending the position rectangle.
To get the actual you poistion you might use this submission from FEX
plotboxpos is perfect, thanks!
For future people who might read this, both the function suggested by Dishant and the plotboxpos function give an array that contains the proportions of the figure window that are taken up by the plot box (white area). Here's how to get the pixel height and width of the plot box:
axesHandles = findall(gcf,'type','axes');
axesPosition=plotboxpos(axesHandles); %This uses the a file exchange function
%axesHandles = findall(h,'type','axes'); %OR this uses a built-in function, which sometimes fails if the plot box is manually changed using functions such as "axis square"
Hi Paul,
I'm raising this thread from the grave to ask a question about your algorithm for determining the width and height.
Shouldn't these instead be:
For axesPosition in default normalized units and windowPosition in default pixel units, using your code with a square axis doesn't give an aspect ratio of 1, whereas the two lines I gave does.
I am wondering if there is a situation where your code is actually what is needed and my code is wrong. I can imagine that if axesPosition was not in normalized units, one would have to do something differently.

Sign in to comment.

Answer by Hieu Nguyen on 9 Feb 2018

You can use the subplot function:
pos = [0.3 0.6 0.2 0.4]; % [left bottom width height]
Then, you can plot the figure that you need:

  1 Comment

Thanks, using subplot is easier...

Sign in to comment.