필터 지우기
필터 지우기

ConstantLine always on the top

조회 수: 78 (최근 30일)
Adib Yusof
Adib Yusof 2020년 11월 29일
댓글: Petr Kryze 2024년 5월 27일
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.

채택된 답변

Adam Danz
Adam Danz 2020년 11월 29일
편집: Adam Danz 2024년 4월 30일
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
Adam Danz
Adam Danz 2024년 4월 30일
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 2024년 5월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Patrick Otto
Patrick Otto 2021년 12월 17일
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
Adam Danz
Adam Danz 2021년 12월 17일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by