[英]How to mark a point in a MATLAB plot?
我有这个情节
[ 全分辨率 ]
我需要在用户输入的 x轴上的一个点上做一条直的垂直线,并显示该垂直线与我的图的交点的坐标 。
如何在MATLAB中完成?
例如:用户输入1020然后将在1020处绘制直线垂直线,其在某点处与绘图相遇并且将以某种方式显示该点的坐标。
一种方法是使用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值只是您正在绘制的数据数组的索引,这可能是您在上面显示的图表中的情况。
尝试类似的东西:
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);
代码未经过测试,并假设您的数字是当前的数字,绘制的数据存储在数组“数据”中,并且原始绘图完成时未指定额外的x向量。
您还可以使用hline
和vline,
函数vline,
可以从以下网址下载: http : //www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline
它们对你来说几乎一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.