Clear Filters
Clear Filters

ConstantLine always on the top

91 views (last 30 days)
Adib Yusof
Adib Yusof on 29 Nov 2020
Commented: Petr Kryze on 27 May 2024 at 10:51
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.

Accepted Answer

Adam Danz
Adam Danz on 29 Nov 2020
Edited: Adam Danz on 30 Apr 2024
Control the layer of a ConstantLine or ConstantRegion
Here are several solutions covering several MATLAB releases.
R2024a or later: set the Layer property
Starting in R2024a, xline, yline, xregion, and yregion all support the ability to be placed behind or in front of objects in an axes using the Layer property. The default for ConstantRegion, the object produced by xregion and yregion, is to appear in the 'bottom' layer and the default for ConstantLine, the underlying object for xline and yline, is to appear in the 'top' layer.
plot(1:10, LineWidth=6);
x1 = xline(2,LineWidth=6,Alpha=1); % default: top
x2 = xline(3,LineWidth=6,Alpha=1,Layer='bottom');
x3 = xregion(4,5,FaceAlpha=1); % default: bottom
x4 = xregion(6,7,FaceALpha=1,Layer='top');
Before R2024a: Use the LimitsChangedFcn with a Line object
Before R2024a, the Layer property was not available. Instead, you can plot a regular line and use the LimitsChangedFcn to update the bounds of the line any time the axis limits change.
% Plot data
v = 1:20;
ax = gca();
hold on
% Add x-line
x = 10;
xl = plot([x,x],ylim(ax), 'k--', 'LineWidth', 4);
% Add y-line
y = 100;
yl = plot(xlim(ax), [y, y], 'k--', 'LineWidth', 4);
% Send x and y lines to the bottom of the stack
% Update the x and y line bounds any time the axes limits change
ax.XAxis.LimitsChangedFcn = @(ruler,~)set(xl, 'YData', ylim(ancestor(ruler,'axes')));
ax.YAxis.LimitsChangedFcn = @(ruler,~)set(yl, 'XData', xlim(ancestor(ruler,'axes')));
Alternative workaround
Lastly, here's the original answer to this question that relys on undocumented features which could change at any time. Here's now to set the ConstantLine to the back of the uistack (Nov 2020). As @Yi-xiao Liu points out, this will not work in Live Scripts.
% 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
Adam Danz
Adam Danz on 30 Apr 2024
This answer was updated on 30-April 2024 to include the R2024a solution that was previously unavailable and to prioritize the line object solution over the undocumented solution.
Petr Kryze
Petr Kryze on 27 May 2024 at 10:51
I was struggling with this so much, until I happened to find this answer by chance. This needs to be better documented! I was screwing around with uistack and Children order and it was driving me nuts.

Sign in to comment.

More Answers (1)

Patrick Otto
Patrick Otto on 17 Dec 2021
In matlab 2019b this workaround doesn't work.
My simple workaround is:
% Set up example
hold on
cLineOwn(gca, 3, 'x', {'color', [0.5,0.5,0.5]});
cLineOwn(gca, 15, 'y', {'color', [0.5,0.5,0.5]});
hold off
% Helper function
function cLineOwn(h, val, ax, opts)
if ax == 'x'
Lim = ylim;
p = plot(h ,[val,val], Lim, opts{:});
uistack(p, "bottom");
elseif ax == 'y'
Lim = xlim;
p = plot(h, Lim, [val,val], opts{:});
uistack(p, "bottom");
  1 Comment
Adam Danz
Adam Danz on 17 Dec 2021
If you're refering to the solution in my answer on this page, it does work in R2019b (see below).
Regarding your solution, the difference between xline and using plot() to create a line is that the xline (or yline) creates a ConstantLine object that updates its position when the axis limits change whereas your plot line does not have that behavior.

Sign in to comment.


Find more on Labels and Annotations in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!