简体   繁体   English

如何在MATLAB图中标记一个点?

[英]How to mark a point in a MATLAB plot?

I have this plot 我有这个情节

[ Full Resolution ] [ 全分辨率 ]

替代文字

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. 我需要在用户输入的 x轴上的一个点上做一条直的垂直线,并显示该垂直线与我的图的交点的坐标

How can this be done in MATLAB? 如何在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. 例如:用户输入1020然后将在1020处绘制直线垂直线,其在某点处与绘图相遇并且将以某种方式显示该点的坐标。

One way to do this is to use the GINPUT function to graphically select a point using the mouse. 一种方法是使用GINPUT功能以图形方式使用鼠标选择一个点。 Assuming the data you plotted is stored in a variable data , the following code should do the sort of thing you want. 假设您绘制的数据存储在可变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

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. 以上假设图表的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. 代码未经过测试,并假设您的数字是当前的数字,绘制的数据存储在数组“数据”中,并且原始绘图完成时未指定额外的x向量。

you can also use the functions hline and vline, which can be downloaded from: http://www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline 您还可以使用hlinevline,函数vline,可以从以下网址下载: http//www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline

They do practically the same for you. 它们对你来说几乎一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM