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


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


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:

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

%Highlight two of these

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.

for ii=1:9
H=get(gcf,'Children'); %get handles of child objects (here, axes) in the figure window


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.