![](/img/trans.png)
[英]How to show co-ordinates of some points on `imagesc` plot in Matlab?
[英]How to mark co-ordinates of points in Octave using “plot” command?
我在 Octave 中使用plot
命令来绘制y
与x
值。 有没有办法使图形还显示绘制在图形本身上的每个坐标的(x,y)
值?
我试过使用help
命令,但找不到任何这样的选项。
另外,如果不可能,有没有办法使用任何其他绘图功能来做到这一点?
您是否尝试过在每个坐标处显示一个文本框?
假设x
和y
是已经存储在 MATLAB 中的坐标,您可以执行以下操作:
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, .......);
然而,如果你想要一些快速和用于说明目的的东西,那么上面的代码就可以了。
这是我用来做类似事情的方法。 为标签生成字符串是最尴尬的部分,真的(我每个标签只有一个数字,这要简单得多)
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\n',[x;y]), '\n');
c(end) = []; % the final \n 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'
来根据自己的喜好调整标签位置。
再想一想,这里有一种替代的、更健壮的方法来生成合适的坐标标签元胞数组:
c = num2cell([x(:) y(:)], 2);
c = cellfun(@(x) sprintf('%.2g,%.2g',x), c, 'UniformOutput', false);
MathWorks File Exchange 中有一个非常有用的包labelpoints可以做到这一点。 它有很多方便的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.