Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
align subplots...magical axes property?

Subject: align subplots...magical axes property?

From: Andrew Stevens

Date: 4 May, 2011 20:30:22

Message: 1 of 3

Hi,

I would like to align a group of subplots where there are various legends and colorbars that seem to be shifting the actual plot box out of alignment. I have tried to set various axes properties ('plotboxaspectratio','outerposition','dataaspectratio') to try to line them up but nothing seems to help. Consider the following ex:

[x,y,z]=peaks(50);

figure
ax(1)=subplot(131);
plot(x(1,:),y(:,1),'-')
legend('something','location','northoutside')

ax(2)=subplot(132);
pcolor(x,y,z)
c1=colorbar('horiz');

ax(3)=subplot(133);
pcolor(x,y,z)

set(ax,'clim',[-5 5],...
    'da',[1 1 1],...
    'xlim',[-3 3],...
    'ylim',[-3 3])

Is there some easy way to line these plots up or do I have to actually go in and set the axes position manually (which will be specific to the paperpostion and plotboxaspectratio)?

Andrew

Subject: align subplots...magical axes property?

From: dpb

Date: 4 May, 2011 23:30:40

Message: 2 of 3

On 5/4/2011 3:30 PM, Andrew Stevens wrote:
> Hi,
>
> I would like to align a group of subplots where there are various
> legends and colorbars that seem to be shifting the actual plot box out
> of alignment. I have tried to set various axes properties
> ('plotboxaspectratio','outerposition','dataaspectratio') to try to line
> them up but nothing seems to help. Consider the following ex:
...

>
> Is there some easy way to line these plots up or do I have to actually
> go in and set the axes position manually (which will be specific to the
> paperpostion and plotboxaspectratio)?

You don't need to do it manually, but you will have to fixup the
positions to match.

You can

pos = get(ax,'position'); % return the axis positions

which will return a cell array of the 3 position vectors and look at the
top/bottom positions of each and set them equal while retaining the
left/right boundaries. That can either be setting the middle one in
this case to the same as the others or fixing them all to an average of
the three or any other fixup you choose, but it can be done
programmatically rather than manually.

The problem is that you're writing other stuff around the various
subplots that isn't the same size and it therefore mungs around to
create the necessary room according to its own logic therefore.

--

Subject: align subplots...magical axes property?

From: Andrew Stevens

Date: 5 May, 2011 00:03:05

Message: 3 of 3

dpb <none@non.net> wrote in message <ipsnit$6rp$1@speranza.aioe.org>...
> On 5/4/2011 3:30 PM, Andrew Stevens wrote:
> > Hi,
> >
> > I would like to align a group of subplots where there are various
> > legends and colorbars that seem to be shifting the actual plot box out
> > of alignment. I have tried to set various axes properties
> > ('plotboxaspectratio','outerposition','dataaspectratio') to try to line
> > them up but nothing seems to help. Consider the following ex:
> ...
>
> >
> > Is there some easy way to line these plots up or do I have to actually
> > go in and set the axes position manually (which will be specific to the
> > paperpostion and plotboxaspectratio)?
>
> You don't need to do it manually, but you will have to fixup the
> positions to match.
>
> You can
>
> pos = get(ax,'position'); % return the axis positions
>
> which will return a cell array of the 3 position vectors and look at the
> top/bottom positions of each and set them equal while retaining the
> left/right boundaries. That can either be setting the middle one in
> this case to the same as the others or fixing them all to an average of
> the three or any other fixup you choose, but it can be done
> programmatically rather than manually.
>
> The problem is that you're writing other stuff around the various
> subplots that isn't the same size and it therefore mungs around to
> create the necessary room according to its own logic therefore.
>
> --

Thanks dpb,

Your advice helped and this is what I came up with:

%get the positons, convert cell to mat
pos=get(ax,'position');
pmat=cat(1,pos{:});

%define new postions
nax=numel(ax);
pnew=repmat(mean(pmat),nax,1);
pnew(:,1)=pmat(:,1); %keep old left pos

%apply new positions
set(ax,{'position'},num2cell(pnew,2))

-Andrew

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us