text alignment in plots ignores margin

4 次查看(过去 30 天)
If I do
h = text(0,0,'hallo','VerticalAlignment','top','margin',10)
I expect that verticalAlignment referes to the top of the box defined by margin. Unfortunately this is not the case. Instead it is alway the top of the text. However, when placing, e.g, text to a marker I would like to add some space between marker and text. Is there any option to accomplish this?
Thank you very much.

回答(1 个)

Jaimin
Jaimin 2024-10-11
You can manually set the margin between the marker and text using the “set” function.
Kindly refer to the following code snippet for better understanding. In this snippet, “x_marker” and “y_marker” represent the <x, y> position of a marker, while “test_height” and “margin” denote the expected height and margin.
set(h, 'Position', [x_marker, y_marker + text_height + margin, 0]);
For more information on “set” function kindly refer following MathWorks Documentation.
I hope this will helpful.
  1 个评论
Ralf Hielscher
Ralf Hielscher 2024-10-11
The problem with this code is, that x,y of the marker are usually in data scale because I want to annotate something in a plot. "textheight" and "margin" on the other hand are in pixels or normalized unit.
In order to make your code work one need to change unit to pixels, make the correction and then switch back, i.e.
h.Units = "pixels";
h.Position = h.Position + [0, offsetY, 0]
h.Units = "data"
Now this works once, but gets out of possition once the figure is resized. Now you can put something like this into a callback function and this eventually works. However, it is very ugly and slow. Especially, the change of unit in the above code takes extremly much time.
All the best,
Ralf.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by