Can I specify the stacking order of graphics objects when using hgtransform objects?

8 views (last 30 days)
I want to control the stacking order of graphics objects in a 2D plot, so that e.g. a line graph appears on top of a rectangle.
As discussed in Answer 93160, this can be done by controlling the order of handles in the Children propert (typically by setting it to a permutation of itself).
However, this does not work when the graphics objects (or a group they are part of) have an hgtransform object as a Parent, e.g. to translate the objects to a different location.
For example, I can make a plot and a filled rectangle which partially overlays it.
x = 0:5:360;
hp = plot(x,sind(x));
hr = rectangle('Position',[180,-1,180,1],'FaceColor','r');
I want the graph to overlay the rectangle, which I can achieve by changing the order of the Children.
set(gca,'Children',[hp; hr]);
The same applies if they are part of an hggroup.
hg = hggroup;
set(hp,'Parent',hg);
set(hr,'Parent',hg);
set(hg,'Children',[hp; hr]);
However, if I use an hgtransform object to translate the objects or the group
TM = makehgtform('translate',[90 1 0]);
hgt = hgtransform('Matrix',TM);
set(hg,'Parent',hgt);
then the rectangle is always on top no matter which order I set the Children objects in.
set(hg,'Children',[hp; hr]);
does not affect the overlay order any more.
Sometimes, part of the plot will appear on top, so this behaviour is not entirely consistent. Am I doing something wrong, or is this a Bug or a 'feature'?

Accepted Answer

Andrew
Andrew on 6 Dec 2013
Lacking other answers, I submitted this as a technical support request / bug report. Their response was that this is not expected bevaviour and is the result of a bug in the (default) 'painters' rendering engine. Hopefully it will be fixed in a future version of matlab.
Meanwhile, the problem can be averted by using a different rendering engine, e.g. by setting
set(gcf,'renderer','zbuffer')
This gives the expected stacking behaviour for graphics objects.
However, if you want to export the figure into a vector format such as EPS, then you are stuck because painters rendering engine is the only one which can do that.

More Answers (1)

Sean de Wolski
Sean de Wolski on 6 Dec 2013
[in reply to your answer]
This isn't really a bug but more of just a limitation in how the painters renderer works. It will draw each patch (i.e. paint them) individually. Thus it does not handle intersecting or stacked patches well because the top patch will be the one you see. I'm not sure on the ordering but in general for this type of application, just use OpenGL.
OpenGL and ZBuffer calculate for each pixel what you would be seeing as a viewer. Because this is calculated pixel by pixel, there aren't vectorized graphics output options..

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!