如何在 MATLAB 绘图中标记点?

发布于 2024-08-06 20:24:56 字数 389 浏览 2 评论 0 原文

我有这个图

[完整分辨率]

alt text

我需要在用户输入显示的 x 轴上的点处制作一条垂直直线该垂直线与我的绘图的交点坐标。

在 MATLAB 中如何做到这一点?

例如:用户输入 1020,然后将在 1020 处绘制一条垂直直线,该直线与绘图的某个点相交,并且将以某种方式显示该点的坐标。

I have this plot

[Full Resolution]

alt text

I need to make a straight vertical line at a point on x axis that the user enters and show the coordinates of the intersection of that vertical line with my plot.

How can this be done in MATLAB?

for example: the user enters 1020 then a straight vertical line will be drawn at 1020 that meets the plot at some point and the coordinates of that point will be shown somehow.

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

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

发布评论

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

评论(3

海拔太高太耀眼 2024-08-13 20:24:56

一种方法是使用 GINPUT 函数使用鼠标以图形方式选择一个点。假设您绘制的数据存储在变量 data 中,以下代码应该执行您想要的操作。

set(gca,'XLimMode','manual','YLimMode','manual');  % Fix axes limits
hold on;
[x,y] = ginput(1);  % Select a point with the mouse
x = round(x);       % Round x to nearest integer value
y = data(x);        % Get y data of intersection
plot([x x],get(gca,'YLim'),'k--');  % Plot dashed line
plot(x,y,'r*');     % Mark intersection with red asterisk
disp('Intersection coordinates:');
disp([x y]);        % Display the intersection point

上面假设图表的 x 值只是您正在绘制的数据数组的索引,从上面显示的图表来看,情况似乎就是这样。

One way to do this is to use the GINPUT function to graphically select a point using the mouse. Assuming the data you plotted is stored in a variable data, the following code should do the sort of thing you want.

set(gca,'XLimMode','manual','YLimMode','manual');  % Fix axes limits
hold on;
[x,y] = ginput(1);  % Select a point with the mouse
x = round(x);       % Round x to nearest integer value
y = data(x);        % Get y data of intersection
plot([x x],get(gca,'YLim'),'k--');  % Plot dashed line
plot(x,y,'r*');     % Mark intersection with red asterisk
disp('Intersection coordinates:');
disp([x y]);        % Display the intersection point

The above assumes that the x-values of the graph are just indices into the array of data you're plotting, which appears to be the case from the graph you show above.

自此以后,行同陌路 2024-08-13 20:24:56

尝试如下操作:

x = 1020;

% plot a vertical line
ylimits = get(gca, 'YLim');
hold on;
plot([x x], ylimits, 'k');

% mark the intersection with the plot
plot(x, data(x), 'ro');
annot = sprintf('Intersection: x=%f, y=%f', x, data(x));
text(x, data(x), annot);

代码未经测试,并假设您的图形是当前图形,绘制的数据存储在数组“data”中,并且原始绘图是在没有指定额外的 x 向量的情况下完成的。

Try something like:

x = 1020;

% plot a vertical line
ylimits = get(gca, 'YLim');
hold on;
plot([x x], ylimits, 'k');

% mark the intersection with the plot
plot(x, data(x), 'ro');
annot = sprintf('Intersection: x=%f, y=%f', x, data(x));
text(x, data(x), annot);

The code is not tested and assumes that your figure is the current one, the plotted data is stored in the array "data" and that the original plot was done without specifying an extra x-vector.

作妖 2024-08-13 20:24:56

您还可以使用函数 hlinevline,,可从以下位置下载:http://www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline

它们实际上为您做同样的事情。

you can also use the functions hline and vline, which can be downloaded from: http://www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline

They do practically the same for you.

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