필터 지우기
필터 지우기

How to draw a "xline" with a given height for the line and a given vertical position for the text?

조회 수: 121 (최근 30일)
How to draw a xline with a given height for the line and a given vertical position for the text?
In other words, I need a shorter xline and I want to decide exactly how short that line will be.
Also, I would like to customise more the vertical position of the text, i.e. putting it a bit higher or a bit lower than what given by 'LabelVerticalAlignment'.

채택된 답변

Star Strider
Star Strider 2022년 3월 11일
The xline function by default goes to the limits if the y-axis. The label only has limited options for positioning.
It will likely be easier to create a single vertical line and attach a text object to it:
x = 1:0.1:10;
y = sin(2*pi*x/5);
figure
plot(x, y)
[xl,xt] = xlin(7,'Message', 0.1, 0.8, 0.25);
grid
function [hl,ht] = xlin(x,txt,ylo,yhi,ytxt)
% Documentation:
% x = x-Position
% txt = Text String
% ylo = Low y-Value (Start)
% yhi = High y-Value (End)
% ytxt = Text Starting Position
hold on
hl = plot([x x],[ylo yhi],'DisplayName',txt, 'LineWidth',1);
ht = text(x,ytxt, txt, 'Horiz','left', 'Vert','top', 'Rotation',90);
hold off
end
Returning the ‘hl’ and ‘ht’ handles permits easily changing certain attributes of the line and text objects without changing the function.
.

추가 답변 (2개)

Voss
Voss 2022년 3월 11일
Here's how you can specify the Vertical and Horizontal Alignment of the xline's label:
warning off all
figure();
xline(1,'_','Line at x = 1','LabelVerticalAlignment','top');
xline(2,'_','Line at x = 2','LabelVerticalAlignment','bottom');
xline(3,'-','Line at x = 3','LabelVerticalAlignment','middle');
xlim([0 4]);
figure();
xline(1,'_','Line at x = 1','LabelHorizontalAlignment','left');
xline(2,'_','Line at x = 2','LabelHorizontalAlignment','right');
xline(3,'_','Line at x = 3','LabelHorizontalAlignment','center');
xlim([0 4]);
If you don't want the xline itself to span the y-limits of the axes, i.e., have an xline of a given height, and/or have more control over where the label is, then you're better off creating a regular line (not an xline) and a text label separately:
figure();
line([1 1],[0 4],'Color','k');
text(1,1.5,'Line at x = 1','Rotation',90,'VerticalAlignment','bottom','HorizontalAlignment','center');
ylim([0 6]);
  댓글 수: 3
Voss
Voss 2022년 3월 11일
편집: Voss 2022년 3월 11일
You're welcome!
Note that it is more efficient to use the low-level function line() than the high-level function plot(), and that plot() can potentially do unwanted things to your axes (or objects in it) that line() will not do. Therefore, I think it is better to use line() for this purpose.
Sim
Sim 2022년 3월 11일
Ah ok, I did not know it....... Can I accept both answers?? I would like to... :-)

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


Walid
Walid 2022년 3월 11일
Please check ConstantLine Properties (frome MATLAB R2021a):
you must use: xline(5,'LabelHorizontalAlignment',''left'') and likewise LabelVerticalAlignment for top, middle or button of the xline.
  댓글 수: 1
Sim
Sim 2022년 3월 11일
편집: Sim 2022년 3월 11일
Thanks @Walid... I am already using those properties.... But, I need to customise them with precise/given heights/positions (not just the usual 'left','right','up','down',...)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by