Change order of Area and Constantline in the same plot

9 Ansichten (letzte 30 Tage)
El
El am 25 Apr. 2025
Kommentiert: El am 25 Apr. 2025
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

Akzeptierte Antwort

Caitlin
Caitlin am 25 Apr. 2025
Bearbeitet: Caitlin am 25 Apr. 2025
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])

Weitere Antworten (2)

Mathieu NOE
Mathieu NOE am 25 Apr. 2025
Bearbeitet: Mathieu NOE am 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])
  1 Kommentar
El
El am 25 Apr. 2025
Thanks! And my mistake, I updated the code so it should now have xi.
This would definitely work... I'm hoping there is a simpler solution that uses some object plotting order property that I would have missed but if not I might use yours!

Melden Sie sich an, um zu kommentieren.


El
El am 25 Apr. 2025
Bearbeitet: El am 25 Apr. 2025
As mentioned in my edit, it turned out that none of the things I tried had any visible effect just because of the high transparency (low alpha) of the area.
In the end, changing the Layer parameter of the xline() function works: Layer = 'top' or Layer = 'bottom' (by default, it seems to go at the top even if plotted before the area). This was suggested in this other post.
Interestingly, uistack doesn't actually change anything to the order of the line and area.
Also, the solution proposed here with
Sxh = struct(this_l); % Get undocumented properties (you'll get a warning)
Sxh.Edge.Layer = 'back'; % Set ConstantLine uistack
works, but the line goes back in front as soon as the window gets resized.

Kategorien

Mehr zu Graphics finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by