Apply plot properties to all MATLAB subplots simultaneously

12,354

Solution 1

The most convenient approach is to create an array of axes handles, and then to set properties:

for i=1:4,
   axesHandles(i) = subplot(2,2,i);
   plot(...)
end

%# set background to black for all handles in the array
%# note that this needs no loop
set(axesHandles,'color','k')

If you don't have the axes handles collected, you need to collect the array of handles first. For this, you can use the children properties of the figure window (gcf gets the handle of the currently active figure)

axesHandles = get(gcf,'children');

and if you have axes across several figures, you can use findall to collect everything:

axesHandles = findall(0,'type','axes');

From then on, it's again a single call to set, or to axis, for example

set(axesHandles,'color','k','lineWidth',2)
axis(axesHandles,'tight')

Solution 2

I can't understand why you think that for loop is evil, but anyhow ...

Here is an answer on part 2 and 3 of your question, assuming that the axes handles were saved in an array:

   a(1) = axes();
   a(2) = axes();
   arrayfun( @(x)(set(x,'Color','r')),a);
   arrayfun( @(x)(axis(x,'equal')),a);

arrayfun applies a function to each and one of the elements in a. Anonymous function in this case is only a shortcut for writing it in the following way:

   a(1) = axes();
   a(2) = axes();
   arrayfun( @SetRedColor ,a);
   arrayfun( @SetAxisEqual,a);

   function SetRedColor(x)
       set(x,'Color','r');
   end

   function SetAxisEqual(x)
       axis(x,'equal');
   end

Another possible way is to link the axes, and set only one property:

   linkprop(a,'Color');
   set(a(1),'Color','r'); %#Now a(2) color is also red
Share:
12,354

Related videos on Youtube

Spacey
Author by

Spacey

[email protected]

Updated on June 04, 2022

Comments

  • Spacey
    Spacey almost 2 years

    I would like to create a figure, and once subplots have been created, I would like to apply properties to all of them simultaneously, without going through a for-loop. In fact, I would like to do all the following without having the need to go through a for-loop:

    • Create all subplots without a for-loop. (For example, create a figure with 4x5 subplots, not using a for-loop).
    • Apply the same background color to each subplot w/o a foor-loop.
    • Apply the same axis command to all of them w/o a for-loop. (Like axis equal, axis tight, etc).

    Is there a way to do this?

  • Spacey
    Spacey over 11 years
    Andrey, its more curiosity about making the code more elegant. Plus this is going to be running inside a for loop anyway, so want to minimize total number of loops. Can you please explain the latter part of your program? How is this applied to all subplots? I understand the anonymous function aspect...
  • Andrey Rubshtein
    Andrey Rubshtein over 11 years
    @Learnaholic, I explained better (I hope). By the way, if it were me designing the code I would do it more elegant by creating a function that does everything related to plots and call it "CreateGUI" or something like this, but that is a matter of personal preference.
  • Spacey
    Spacey over 11 years
    thank you, I get everything except: 1) a(1) = axes() aspect of it. Where exactly are my subplot handles, is that a(1), etc? Why are you equating them to axes() like this? I dont get this. 2) I have a similar 'plotEverything' function, but I am updating plots on it at every iteration within another loop. (Optimization problem). Thanks.
  • Andrey Rubshtein
    Andrey Rubshtein over 11 years
    @Learnaholic, yes, a(1) is the handle of the subplot. subplot is just a fancy call to axes with adjusted position. You can definitely change this line to a(1) = subplot(...)
  • Jonas
    Jonas over 11 years
    There's no need to call arrayfun, by the way. set accepts arrays as input.
  • Andrey Rubshtein
    Andrey Rubshtein over 11 years
    @Learnaholic, definetely check out Jonas answer as well, it is more elegant.
  • Spacey
    Spacey over 11 years
    Jonas, thanks. So I take it there is no way to create the subplots without a loop though?
  • Jonas
    Jonas over 11 years
    @Learnaholic: No, axes and subplot can only create one set of axes at a time. You can, if you want, hide the loop by using subaxis from the File Exchange (this also conveniently lets you set spacing between axes).