How to find the angle of a line with respect to plot window

조회 수: 3 (최근 30일)
himura
himura 2023년 3월 23일
편집: himura 2023년 3월 23일
This is a little hard to explain. I am not looking for the angle of the line with respect to the cartesian coordinates, I am looking for the angle with respect to the plot window itself.
For context, I am trying to use this angle to produce some text that has the same angle as the line
For Example:
plot([0:10], [0:10])
text( 0, 0, "----------------------some text", Rotation = 45)
p = subplot(1,1,1);
disp(p.Position(3:4))
0.7750 0.8150
See how the text angle doesn't match the line angle, and the plot itself is not a square
i- I'd imagine some calculations are in order, and it would need the size of the window as an input.
ii- I also thought that ths cannot be that complex, surely there's a way to write text on the plot at an angle relative to the plot's coordiates, right?
If anyone has someinsight on (i) or (ii), that would be highly appriociated!

채택된 답변

Joe Vinciguerra
Joe Vinciguerra 2023년 3월 23일
Try the PlotBoxAspectRatio property.
plot([0:10], [0:10])
ar = get(gca, "PlotBoxAspectRatio")
ar = 1×3
1.0000 0.7897 0.7897
r = atand(ar(2)/ar(1)) % This will give you the angle of the diagonal of the axes in degrees
r = 38.2985
text( 0, 0, "----------------------some text", Rotation = r)
Then compare it to the slope of the line you are plotting.

추가 답변 (1개)

Catalytic
Catalytic 2023년 3월 23일
편집: Catalytic 2023년 3월 23일
For context, I am trying to use this angle to produce some text that has the same angle as the line
There are File Exchange posts that already do this kind of thing, for example this one.
  댓글 수: 1
himura
himura 2023년 3월 23일
편집: himura 2023년 3월 23일
Thank you! Very good reference. I used the source code to figure out other things. Highly recommended

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by