recipes : Plotting : Plot Manipulation : Modifying sub-plot panels after they've been created

Problem

You want to modify the axis properties of sub-plot panels after having made them.

Solution

There are two approaches to this. The first is to make a record of the sub-plot handles as they're created. Then modify the handles as desired. The second is to obtain the handles post-hoc. The first approach may be more simple but it's useful to also know the second. Note that a "handle" is just a number that Matlab uses to provide access to a resource such as window, a plot element, or a piece of hardware.

Example where handles are recorded we go:

clf
%Make for sub-plots, each containing a histogram
for ii=1:4
    H(ii)=subplot(2,2,ii); %H contains axis handle
    hist(randn(1,100),20)
end

%Highlight two of these
set(H([2,4]),'LineWidth',3,'color',[1,0.9,0.9])

In this second example we will make the subplots as before but this time not record the axis handles. We will then recover the handles and modify the plots. In this case, we highlight every other panel.

clf
for ii=1:9
    subplot(3,3,ii);
    plot(randn(1,100),'-k')
end
H=get(gcf,'Children'); %get handles of child objects (here, axes) in the figure window
set(H([1:2:length(H)]),'LineWidth',2,'color',[0.8,1,0.8])

Discussion

Any plot object can be modified in any way after it has been created. Recording the handles, as in the first example, is the most foolproof approach since you always know which axis handles are being stored. The second approach of finding the axis handles allows us to write a program that can be applied to any figure window, even if you forgot to record the axis handles. In practice this may require some error checking (e.g. to make sure that all the handles returned are for figure axes) but it does provide for flexibility. The reason it may require error checking is because the "children" of the current figure may be something other than regular sub-plots. For example, they could be color-bars from a surface plot.