Change order of Area and Constantline in the same plot
3 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Caitlin
2025 年 4 月 25 日
編集済み: Caitlin
2025 年 4 月 25 日
Your line is behind the area. Your alpha value is making the area see through, so you can see the line. If you change alpha to 1 (no transparency), it's easier to see. I also changed the face color to try and make it the color you like (not close but you get the point lol).
xi = linspace(-20,20,100);
%% Parameters
nbins = 100;
min_value = -5; % min possible value of curve
max_value = 10; % max possible value of curve
alph = 1;
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
fig = 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",[0.86 0.78 0.93],'EdgeColor','none','FaceAlpha', alph);
xlim([-20, 20])
その他の回答 (2 件)
Mathieu NOE
2025 年 4 月 25 日
編集済み: Mathieu NOE
2025 年 4 月 25 日
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])
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

