recipes : Plotting : Plot Manipulation : Creating complicated arrangements of sub-plots

Problem

You want to plot multiple panels in a complex arrangment within one figure window.

Solution

Use the axes command. For example:

nPanels=10; %the number of panels
panelWidth=0.15; %Width of each panel as a proportion of the figure
panelHeight=0.10; %Height of each panel as a proportion of the figure

xMax=1-panelWidth;
yMax=1-panelHeight;
x=0.05:xMax/nPanels:xMax;
y=0.05:xMax/nPanels:xMax;

clf
for ii=1:nPanels
    axes('Position',[x(ii),y(ii),panelWidth,panelHeight])
    hist(randn(1,100),20)
    box on
    set(gca,'XTick',[],'YTick',[]) %remove x and y ticks
end

Discussion

axes allows plots to overlap and allows them to be positioned freely within a figure window. The location and size of the sub-panel are defined by a 4 element vector. The 4 elements in the vector are:

  1. The position of the lower-left corner in x
  2. The position of the lower-left corner in y
  3. The width of the panel
  4. The height of the panel
All dimensions go from 0 to 1. For the position coordinates, [0,0] refers to the bottom left of the figure window and [1,1] to the top right. The size of the sub-panel is defined as a proportion of the size of the figure window. So 0.5 would mean half the size of the figure window. To make a panel that stretches all the way across the middle third of the figure window we would do: axes('position',[0,0.33,1,0.33])