ConstantLine always on the top

66 vues (au cours des 30 derniers jours)
Adib Yusof
Adib Yusof le 29 Nov 2020
Commenté : Petr Kryze le 27 Mai 2024
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.

Réponse acceptée

Adam Danz
Adam Danz le 29 Nov 2020
Modifié(e) : Adam Danz le 30 Avr 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
figure()
v = 1:20;
plot(v,v.^2,'-c','LineWidth',6)
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
uistack([xl,yl],'bottom')
% 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
figure()
plot(magic(5),'LineWidth',5)
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
  9 commentaires
Adam Danz
Adam Danz le 30 Avr 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 le 27 Mai 2024
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Patrick Otto
Patrick Otto le 17 Déc 2021
In matlab 2019b this workaround doesn't work.
My simple workaround is:
% Set up example
clf()
hold on
plot(magic(5),'LineWidth',5)
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");
end
end
  1 commentaire
Adam Danz
Adam Danz le 17 Déc 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Labels and Annotations dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by