Draw a line between a point, which has non-normalised coordinates, and a legend, which has a normalised position

3 次查看(过去 30 天)
I want to draw a line between a point (which has non-normalised coordinates) and a legend (which has a normalised position).
xp = 34573;
yp = 89832;
p = plot(xp,yp,'o','MarkerFaceColor','b');
lgd = legend(p);
It would be easy if both the point coordinates and the legend position were either both normalised or both non-normalised. Indeed, I would like to use something similar to this:
plot([xp lgd.Position(1)],[yp lgd.Position(2)])
but it does not work, since they have a different normalization.
Therefore, is there a way to transform either the non-normalised coordinates into normalised coordinates, or viceversa?

采纳的回答

Star Strider
Star Strider 2024-8-17
I wrote my osn set of utility functions for my own use for these sorts of problems. The approach I use here is to find the normalised coordinates of the point, and use the ‘left’ and ‘lower’ legend position values to draw the annotation arrow.
Try this —
xp = 34573;
yp = 89832;
p = plot(xp,yp,'o','MarkerFaceColor','b');
lgd = legend(p);
lgdpos = lgd.Position;
xapf = @(x,pos,xl) pos(3)*(x-min(xl))/diff(xl)+pos(1); % 'x' Annotation Position Function
yapf = @(y,pos,yl) pos(4)*(y-min(yl))/diff(yl)+pos(2); % 'y' Annotation Position Function
xl = xlim;
yl = ylim;
pos = gca().Position;
pointpos = [xapf(xp,pos,xlim), yapf(yp,pos,ylim)];
annotation('arrow', [pointpos(1) lgdpos(1)], [pointpos(2) lgdpos(2)])
Make appropriate changes to get the result you want.
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by