matlab绘图中的注释

发布于 2024-08-28 13:09:52 字数 464 浏览 2 评论 0原文

我只是想知道如何在matlab图中添加注释?这是我的代码:

plot(x,y);  
annotation('textarrow',[x, x+0.05],[y,y+0.05],'String','my point','FontSize',14);

但是箭头指向错误的地方。我该如何修复它?还有更好的注释情节的想法吗?

谢谢和问候!


编辑:

我刚刚从帮助文档中看到:

annotation('line',x,y) 创建一个线注释对象,该对象从 x(1),y(1) 定义的点延伸到 x(2),y(2) 定义的点,指定采用标准化数字单位。

在我的代码中,我希望箭头指向由plot()绘制的点(x,y),但注释将x和y的值解释为标准化图形单位。所以我认为这就是导致问题的原因。如何指定注释的正确坐标?

I just wonder how to add annotation in matlab plot? Here is my code:

plot(x,y);  
annotation('textarrow',[x, x+0.05],[y,y+0.05],'String','my point','FontSize',14);

But the arrow points to the wrong place. How can I fix it? And any better idea for annotating a plot?

Thanks and regards!


EDIT:

I just saw from the help document:

annotation('line',x,y) creates a line annotation object that extends from the point defined by x(1),y(1) to the point defined by x(2),y(2), specified in normalized figure units.

In my code, I would like the arrow pointing to the point (x,y) that is drawn by plot(), but annotation interprets the values of x and y as in normalized figure units. So I think that is what causes the problem. How can I specify the correct coordinates to annotation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事还在继续 2024-09-04 13:09:52

首先,您需要找到标准化图形单位中轴的位置。幸运的是,它们默认设置为“标准化”。

axPos = get(gca,'Position'); %# gca gets the handle to the current axes

axPos 是 [xMin,yMin,xExtent,yExtent]

然后,您将获得限制,即轴的最小值和最大值。

xMinMax = xlim;
yMinMax = ylim;

最后,您可以根据绘图 x 和 y 计算注释 x 和 y。

xAnnotation = axPos(1) + ((xPlot - xMinMax(1))/(xMinMax(2)-xMinMax(1))) * axPos(3);
yAnnotation = axPos(2) + ((yPlot - yMinMax(1))/(yMinMax(2)-yMinMax(1))) * axPos(4);

使用 xAnnotation 和 yAnnotation 作为注释的 x 和 y 坐标。

First, you need to find the position of the axes in normalized figure units. Fortunately, they're set to 'normalized' by default.

axPos = get(gca,'Position'); %# gca gets the handle to the current axes

axPos is [xMin,yMin,xExtent,yExtent]

Then, you get the limits, i.e. min and max of the axes.

xMinMax = xlim;
yMinMax = ylim;

Finally, you can calculate the annotation x and y from the plot x and y.

xAnnotation = axPos(1) + ((xPlot - xMinMax(1))/(xMinMax(2)-xMinMax(1))) * axPos(3);
yAnnotation = axPos(2) + ((yPlot - yMinMax(1))/(yMinMax(2)-yMinMax(1))) * axPos(4);

Use xAnnotation and yAnnotation as x and y coordinates for your annotation.

夜空下最亮的亮点 2024-09-04 13:09:52

获取标准化图形坐标的另一种方法是使用 Data空间到数字单位转换 (ds2nfu) 在 FileExchange 上提交。

[xa ya] = ds2nfu(x,y);

Another way to get normalized figure coordinates is to use Data space to figure units conversion (ds2nfu) submission on FileExchange.

[xa ya] = ds2nfu(x,y);
梦醒灬来后我 2024-09-04 13:09:52

我在理解标准化坐标时遇到了一些困难,直到我意识到坐标 (0,0) 和 (1,1) 分别是完整绘图窗口的左下角和右上角,而不仅仅是绘图的左下角和右上角。下面的代码片段和屏幕截图可能会帮助那些想知道 0 开始和 1 结束的人。

x = -1:0.1:1;
y = x.^2;
plot (x,y)
xlabel('time [s]')
ylabel('amplitude')
title('My nice plot')
legend('y(t)')
grid on
annotation('arrow', [0 1], [0 1])

使用箭头坐标 (0,0) 和 (1,1) 进行绘图

I had some trouble understanding the normalized coordinates, until I realized that the coordinates (0,0) and (1,1) are respectively the lower left corner and upper right corner of the COMPLETE plot window, not just of the plot. The below snippet and the screenshot might help others who have been wondering where the 0 starts and the 1 ends.

x = -1:0.1:1;
y = x.^2;
plot (x,y)
xlabel('time [s]')
ylabel('amplitude')
title('My nice plot')
legend('y(t)')
grid on
annotation('arrow', [0 1], [0 1])

Plot with arrow coordinates (0,0) and (1,1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文