关于matlab:如何使用”绘图”命令在Octave中标记点的坐标?

How to mark co-ordinates of points in Octave using “plot” command?

我在Octave中使用plot命令来绘制yx值。有没有办法使图形还显示图形本身上绘制的每个坐标的(x,y)值?

我尝试使用help命令,但找不到任何此类选项。

如果不可能,是否有其他方法可以使用此功能?


您是否尝试过在每个坐标处显示一个文本框?

假设xy是已经存储在MATLAB中的坐标,则可以执行以下操作:

1
2
3
4
plot(x, y, 'b.');
for i = 1 : numel(x) %// x and y are the same lengths
    text(x(i), y(i), ['(' num2str(x(i)) ',' num2str(y(i)) ')']);
end

上面的代码将获取图形中的每个点,并以(x,y)的格式放置一个文本框(无边框),其中x和y是所有点的坐标。

注意:您可能必须在文本框的位置上随意移动,因为上面的代码会将每个文本框放在每对坐标的顶部。您可以通过在text函数的第一个和第二个参数中添加/减去适当的常量来进行操作。 (即text(x(i) + 1, y(i) - 1, .......);,但是,如果您希望快速进行操作并出于说明目的,那么上面的代码就可以了。


有一个非常有用的package标签点
在MathWorks File Exchange中可以做到这一点。
它具有许多方便的功能。


这是我用来做类似事情的一种方法。确实,为标签生成字符串是最尴尬的部分(每个标签我只有一个数字,这要简单得多)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
x = rand(1, 10);
y = rand(1, 10);
scatter(x, y);

% create a text object for each point
t = text(x, y, '');

% generate a cell array of labels - x and y must be row vectors in this case
c = strsplit(sprintf('%.2g,%.2g\
',[x;y]), '\
');
c(end) = [];  % the final \
 gives us an extra empty cell, remove it
c = c';  % transpose to match the dimensions of t

% assign each label to each text object
set(t, {'String'}, c);

您可能想玩各种属性,例如'HorizontalAlignment''VerticalAlignment''Margin',以根据自己的喜好调整标签位置。

经过一番思考后,这是生成合适的坐标标签单元格数组的另一种更健壮的方法:

1
2
c = num2cell([x(:) y(:)], 2);
c = cellfun(@(x) sprintf('%.2g,%.2g',x), c, 'UniformOutput', false);