Change order of Area and Constantline in the same plot
Show older comments
Hi all,
Edit: it turned out the line was behind the area from the start; it just looked like it was in front because of the low alpha. My mistake!
With an updated alph = 0.9 in the code below, you can clearly see that the line is actually at the back:

The position of xline is set by its Layer parameter: using either Layer = 'top' or Layer = 'bottom' works to shift it, here it is with Layer = 'top':

So... all is well. Will know to check that for next time!
### Previous post:
I am trying to plot an area and a ConstantLine (xline) objects on the same plot. I would like the line to be behind the area. However it seems impossible to get this, instead the line keeps staying in front of the area, no matter how I try to change it (e.g. with uistack). I have found many posts related to similar problems here but none that solve it.
Here is my example:
%% Parameters
nbins = 100;
min_value = -5; % min possible value of curve
max_value = 10; % max possible value of curve
alph = 0.2;
bw = 0.5;
%% Data that will be plotted
xi = linspace(min_value,max_value,nbins); % where the density will be estimated
bin_size = median(diff(xi));
x1 = normrnd(1,1,[200,1]);
f1 = ksdensity(x1,xi,"Bandwidth",bw,'Function','pdf');
%% Plotting
figure()
% plot the line
% Note, I try to set the 'Layer' property to bottom here as in https://www.mathworks.com/matlabcentral/answers/667763-constantline-always-on-the-top?s_tid=srchtitle
% but it still plots it 'on top' of the next plot which is an area and
% doesn't accept a 'Layer' option
this_l = xline(2.5, 'k', Layer='bottom');
hold on
% plot the area
this_a = area(xi,f1,0,"FaceColor",'b','EdgeColor','none','FaceAlpha', alph);
xlim([-20, 20])
% => This shows the line on top of the area, even though the area was plotted
% after
%% Attempts to change order
% Trying uistack to switch the order
uistack(this_a, 'top');
uistack(this_l, 'bottom');
% This doesn't change anything
% Trying this solution:
% https://www.mathworks.com/matlabcentral/answers/667763-constantline-always-on-the-top?s_tid=srchtitle
Sxh = struct(this_l); % Get undocumented properties (you'll get a warning)
Sxh.Edge.Layer = 'back'; % Set ConstantLine uistack
% This doesn't do anything
This is the result:

Any suggestions on how to get this line to be behind the area? Thank you!
- El
Accepted Answer
More Answers (2)
Mathieu NOE
on 25 Apr 2025
Edited: Mathieu NOE
on 25 Apr 2025
hello
xi was missing so I created a dummy one
maybe not the smartest solution, but it does the job :
%% Parameters
xi = linspace(-20,20,100);
nbins = 100;
min_value = -5; % min possible value of curve
max_value = 10; % max possible value of curve
alph = 0.2;
bin_size = median(diff(xi));
bw = 0.5;
%% Data that will be plotted
x1 = normrnd(1,1,[200,1]);
f1 = ksdensity(x1,xi,"Bandwidth",bw,'Function','pdf');
%% Plotting
figure()
hold on
% plot the line
x_line = 2.5;
y_line = interp1(xi,f1,x_line,'next');
plot([x_line x_line],[y_line max(f1)*1.2],'k');
% plot the area
this_a = area(xi,f1,0,"FaceColor",'b','EdgeColor','none','FaceAlpha', alph);
xlim([-20, 20])
Categories
Find more on Graphics 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!
