MATLAB Answers

ConstantLine always on the top

4 views (last 30 days)
Adib Yusof
Adib Yusof on 29 Nov 2020
Commented: Adam Danz on 2 Dec 2020
I have been noticing that ConstantLine, which is plotted by xline() or yline() functions, always located on the top of other objects in an axes, no matter whether I plot the ConstantLine before or after other objects (e.g., Line, Scatter)
Even rearranging it by using uistack() like this did nothing:
uistack(ConsLine, 'bottom')
I have checked that all objects are the children of the axes, therefore, I should be able to rearrange it by uistack(), right?
I have also tried setting the HandleVisibility property to 'on' before uistack-ing, but nothing happened:
set(allchild(gca), 'HandleVisibility', 'on');
uistack(ConsLine, 'bottom')
I could not find anything on the documentation or here on MATLAB Answers regarding this behaviour.
Please help. I'm using MATLAB R2020b. Thank you so much.


Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 29 Nov 2020
Edited: Adam Danz on 29 Nov 2020
It's undocumented but here's now to set the ConstantLine to the back of the uistack.
% Set up example
xh = xline(3,'LineWidth',8,'Alpha',1);
% Set ConstantLine to 'back'
% Temporarily disable the warning that appears when accessing
% the undocumented properties.
warnState = warning('off','MATLAB:structOnObject');
cleanupObj = onCleanup(@()warning(warnState));
Sxh = struct(xh); % Get undocumented properties (you'll get a warning)
clear('cleanupObj') % Trigger warning reset
Sxh.Edge.Layer = 'back'; % Set ConstantLine uistack


Adib Yusof
Adib Yusof on 30 Nov 2020
It works perfectly, thank you!
Albert Bing
Albert Bing on 2 Dec 2020
Any chance of doing it in the older version of MATLAB without using xline?
xline is a ConstantLine object, the Sxh.Edhe.Layer is a LineStrip object, while the normal Line object doesn't have that.
Adam Danz
Adam Danz on 2 Dec 2020
This workaround is not needed with lines produced by plot() or line() since those objects are supported by uistack.
h = plot([x,x],ylim());

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!