Placing annotations at specific xy coordinates

Just wondering but I wanted to put annotations/text boxes at specific coordinates but when I read up on the page I saw that the values of x and y in the 'dim' vector must be between 0 and 1. Im just looking for a way to place 'X's at specific xy coordinates to signify that those points are no longer being considered for my calculations?? I figured annotations would be good way to accomplish that because I wanted to avoid matlab cconsidering a plotted X as an active point and therefore valid for my calculations. If you have any work arounds or suggestions Im all ears. Thanks for the help

 採用された回答

Adam Danz
Adam Danz 2019 年 9 月 13 日
編集済み: Adam Danz 2025 年 4 月 21 日

0 投票

Annotation objects are designed to annotation the figure space in figure coordinates. They are not designed to be positioned within axes coordinates. For axes and data coordinates, using axes objects such as rectangle or text.
hold on
plot(x,y,'rx')
If you wanted rectangles for whatever reason,
widthHeight = [.1,.1];
rectangle('Position', [x-widthHeight(1)/2, y-widthHeight(2)/2, widthHeight])

5 件のコメント

Vance Blake
Vance Blake 2019 年 9 月 14 日
I just wanted to avoid the possible confusion but occam's razor rules the day again. Thanks to both of you for talking me off that ledge. Seems like I was really making my life difficult.
Adam Danz
Adam Danz 2019 年 9 月 14 日
As Image Analyst said, you could always use text() which uses the same units as your data.
text(x,y,'X','HorizontalAlignment','Center','VerticalAlignment','Middle',...
'FontSize', 12,'FontWeight','bold','Color', 'r')
Vance Blake
Vance Blake 2019 年 9 月 14 日
Yeah Im reading up on it now cause I didnt know that it existed. Feel like that link should be a related topic at the bottom of the annotation page but that's splitting hairs. Thanks again!
Adam Danz
Adam Danz 2019 年 9 月 14 日
Vance Blake
Vance Blake 2019 年 10 月 16 日
Hey Adam how are you ? Its been a while but I have some more questions that I would really like your help with when you get the chance the link to the question is below. https://www.mathworks.com/matlabcentral/answers/485730-define-an-if-statement-based-on-on-finding-duplicate-values-between-arrays-and-then-rewrite-code-to

サインインしてコメントする。

その他の回答 (4 件)

Olle Trollberg
Olle Trollberg 2023 年 8 月 29 日

3 投票

I recently ran into this and here is a simple way to get around it using some anonymous functions to do rescaling.
Works at least in 2022b
plot(x,y) % Create whatever plot
ax = gca;
% Define helper funcitons to normalize from axis coordinates to normalized position in figure.
xnorm = @(x)((x-ax.XLim(1))./(ax.XLim(2)-ax.XLim(1))).*ax.InnerPosition(3)+ax.InnerPosition(1)
ynorm = @(y)((y-ax.YLim(1))./(ax.YLim(2)-ax.YLim(1))).*ax.InnerPosition(4)+ax.InnerPosition(2)
annotation('textarrow',xnorm(x_in_axis_coordinates),ynorm(y_in_axis_coordinates),'string','Arrow Text');
Image Analyst
Image Analyst 2019 年 9 月 13 日

1 投票

No, not true. If you want to place annotation text onto a plot you can use text() using the x,y that's used for that plot, whatever range it might have, which can be more than 1.
Have you tried
badIndexes = .......whatever you need to do to identify "points are no longer being considered for my calculations"
hold on
plot(x(badIndexes), y(badIndexes), 'rx', 'MarkerSize', 15, 'LineWidth', 2) % Overlay big red X's
Bruno Luong
Bruno Luong 2019 年 9 月 14 日

0 投票

There are several tool on File Exchange, such as this one
Dmitrij Usov
Dmitrij Usov 2025 年 4 月 16 日

0 投票

f = figure;
an = annotation('arrow');
an.Parent = f.CurrentAxes;
an.X = [0 1];
an.Y = [0 1];

3 件のコメント

DGM
DGM 2025 年 4 月 16 日
編集済み: DGM 2025 年 4 月 16 日
I didn't even know an annotation could be the descendant of an axes. That's a tremendous usability simplification, but it does come with a complication that may limit its use. As a child of the axes, it will be subject to clipping. Some annotation elements can't hang outside the plot box.
% some fake data to plot
x = linspace(0,1.5,100);
plot(x,x.^2); hold on
plot(x,sqrt(x))
xlim([0 1.5])
ylim([0 1.5])
% a text box hanging out of the axes
an1 = annotation('textbox');
an1.Parent = gca;
an1.String = 'the curves cross here';
an1.HorizontalAlignment = 'center';
an1.VerticalAlignment = 'middle';
an1.Position = [1.1 0.95 0.6 0.08];
an1.BackgroundColor = [1 1 0];
% a text arrow doing the same
an2 = annotation('textarrow');
an2.Parent = gca;
an2.String = 'the curves cross here';
an2.HorizontalAlignment = 'left';
an2.VerticalAlignment = 'top';
an2.X = [1.15 1];
an2.Y = [0.8 1];
% another arrow pointing to something beyond ylim
an3 = annotation('textarrow');
an3.Parent = gca;
an3.String = 'this line keeps going';
an3.HorizontalAlignment = 'center';
an3.VerticalAlignment = 'top';
an3.X = [0.5 1.5];
an3.Y = [1.1 1.5^2];
So things like text will not be clipped, but the boxes and arrows will. We can set the 'clipping' property of the axes to 'off', but that will mean that plotted data (e.g. the blue curve) will also not be clipped.
That said, I don't know how often we really need to hang annotations outside the plot box.
Adam Danz
Adam Danz 2025 年 4 月 21 日
Also watch out for axes direction.
Note that assigning axes to the annotation's parent is undocumented.
% some fake data to plot
x = linspace(0,1.5,100);
plot(x,x.^2); hold on
plot(x,sqrt(x))
xlim([0 1.5])
ylim([0 1.5])
set(gca,'YDir','Reverse') % <-------------------Added
% a text box hanging out of the axes
an1 = annotation('textbox');
an1.Parent = gca;
an1.String = 'the curves cross here';
an1.HorizontalAlignment = 'center';
an1.VerticalAlignment = 'middle';
an1.Position = [1.1 0.95 0.6 0.08];
an1.BackgroundColor = [1 1 0];
% a text arrow doing the same
an2 = annotation('textarrow');
an2.Parent = gca;
an2.String = 'the curves cross here';
an2.HorizontalAlignment = 'left';
an2.VerticalAlignment = 'top';
an2.X = [1.15 1];
an2.Y = [0.8 1];
% another arrow pointing to something beyond ylim
an3 = annotation('textarrow');
an3.Parent = gca;
an3.String = 'this line keeps going';
an3.HorizontalAlignment = 'center';
an3.VerticalAlignment = 'top';
an3.X = [0.5 1.5];
an3.Y = [1.1 1.5^2];
DGM
DGM 2025 年 4 月 22 日
Oh wow. That is a problem.

サインインしてコメントする。

カテゴリ

質問済み:

2019 年 9 月 13 日

コメント済み:

DGM
2025 年 4 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by