What determines the stacking order of objects in MATLAB?

18 views (last 30 days)
My figure requires certain objects to appear on top of certain other objects, so that (for instance) one patch is shown "in front" of another.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Jan 2024
Edited: MathWorks Support Team on 24 Jan 2024
The stacking order works according to one of the following rules:
1) If the objects front-back order can be distinguished by their location in three-dimensional space, they appear "in front of" or "in back of" each other according to the natural way one would expect the scene to appear. That is, those closer to the camera are in front, those farther away are behind.
Note that this applies even when you are producing a figure that looks two-dimensional. Axes in MATLAB represent three dimensional space, even if they are viewed from one side as with
view(2)
or the default axes view.
There is an exception to rule (1): If the 'SortMethod' is set to 'childorder', objects in that axes are drawn according to rule (2), despite the fact that rule (1) could have been used.
2) If the objects front-back order cannot be distinguished by their location in three-dimensional space, then rule (1) does not apply.
For instance, this occurs with coplanar, overlapping patches (which have parts colocated in space), lines created by PLOT (whose 'ZData' is empty), and UICONTROLS (which have only an X-Y location).
In this case, the objects are ordered front-to-back according to the location of their handles in the parent object's 'Children' list. Those objects whose handles appear near the front of the list are in front of those whose handles appear near the end.
This list can be manipulated manually with GET and SET, or by use of the UISTACK function. But it is perhaps most often controlled by controlling the order in which the objects are created. Objects' handles are added to the front of the 'Children' list as they are created, so objects created later show up on top.
There are some exceptions to rule (2):
a) If a line is coplanar with a patch or surface and the zbuffer or OpenGL renderer is being used, the lines appear on top. This also applies to the lines that form the edges of patches and surfaces.
b) UICONTROL objects always display on top of AXES objects.
c) When an AXES object is not in the standard two-dimensional view, such as with
view(3);
the ordering implied by the 'Children' list may not be honored, the results are somewhat undefined, and very renderer-dependent.
Please note that the axes stacking order is defined by its 'SortMethod' property. This property defaults to 'depth', which shows graphics back to front based on the current view. You may want to change this property to 'childorder', which shows graphics based on their order in the axes Children, and allows you to manually control what is shown on top.

More Answers (0)

MathWorks Support

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!