text box for 'xline' function?

조회 수: 14 (최근 30일)
Fercho_Sala
Fercho_Sala 2021년 6월 2일
댓글: Scott MacKenzie 2021년 6월 2일
Hello, (probably this is an easy question) using the function ‘xline’ in order to represent a statistical value, how can I add a text box with its respective value pointing with an arrow the middle of the line? Thanks.

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 6월 2일
편집: Scott MacKenzie 2021년 6월 2일
There are a few ways to do this. With annotation, it's easy to combine an arrow with a text box. The downside is you have to use normalized coordinates, rather than data coordinates:
ax = gca;
xValue = 5;
axis([0, 10, 0, 10]);
xline(xValue);
x2 = xValue / ax.XLim(2) * ax.Position(3) + ax.Position(1);
x1 = x2 - 0.1; % on left side of line
y2 = .5; % middle of line in y-axis
y1 = .5; % middle of line in y-axis
annotation('textarrow', [x1 x2], [y1 y2], 'String', sprintf('x = %d ', xValue));
  댓글 수: 3
Rik
Rik 2021년 6월 2일
편집: Rik 2021년 6월 2일
You only need to adapt the x coordinates based on the xValue:
xValue=5;
axis([0, 10, 0, 10]);
[-0.1 .02]+(xValue-min(xlim))/diff(xlim)
ans = 1×2
0.4000 0.5200
Scott MacKenzie
Scott MacKenzie 2021년 6월 2일
I tweaked the answer "without knowing the coordinates". Of course, there will be issues if the x value is close to the left side of the plot. You can add some logic, if you wish, to position the text box and arrow on the right or left side of the line depending on the value of x.

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by