如何在 Matlab 中标记两个向量?

发布于 2024-08-21 06:12:04 字数 188 浏览 2 评论 0原文

我有一个 2 列矩阵(称为 M,我使用 Matlab 的 plot 命令(plot(M))将其可视化为两个向量。我有 :

  1. 我想在绘图上标记向量本身。
  2. 我想在绘图上标记矩阵的每一行(即每个向量分量)。

两个问题

I have a 2 column matrix(called M, which I visualize as two vectors using Matlab's plot command(plot(M)). I have two issues:

  1. I want to label the vectors themselves on the plot.
  2. I want to label each row of the matrix(i.e. each vector component) on the plot.

How would I go about doing those things?

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

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

发布评论

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

评论(3

野味少女 2024-08-28 06:12:04

示例:

M = cumsum(rand(10,2) - 0.5);
x = 1:size(M,1);
plot(x, M(:,1), 'b.-', x, M(:,2), 'g.-')
legend('M1', 'M2')
for i=x
    text(i+0.1, M(i,1), sprintf('%.2f', M(i,1)), 'FontSize',7, 'Color','b');
    text(i+0.1, M(i,2), sprintf('%.2f', M(i,2)), 'FontSize',7, 'Color','g');
end

plot

或者,您可以使用:

datacursormode()

这将使用户能够 指向并单击点以查看数据标签。

An example:

M = cumsum(rand(10,2) - 0.5);
x = 1:size(M,1);
plot(x, M(:,1), 'b.-', x, M(:,2), 'g.-')
legend('M1', 'M2')
for i=x
    text(i+0.1, M(i,1), sprintf('%.2f', M(i,1)), 'FontSize',7, 'Color','b');
    text(i+0.1, M(i,2), sprintf('%.2f', M(i,2)), 'FontSize',7, 'Color','g');
end

plot

Alternatively, you can use:

datacursormode()

which will enable the user to just point and click on points to see the data labels.

巴黎盛开的樱花 2024-08-28 06:12:04

您可能需要对此进行调整,以使标签的位置完全符合您的要求,但类似这样的方法就可以解决问题。

M = [1 2; 3 4; 5 6]
plot(M)
nrows = size(M, 1);
ncols = size(M, 2);
x = repmat(nrows - .3, 1, ncols);
y = M(end, :) - .3;
labels = cellstr([repmat('Col', ncols, 1), num2str((1:ncols)')]);
text(x, y, labels)

You may need to tweak this to get the positions of the labels exactly how you want them, but something like this will do the trick.

M = [1 2; 3 4; 5 6]
plot(M)
nrows = size(M, 1);
ncols = size(M, 2);
x = repmat(nrows - .3, 1, ncols);
y = M(end, :) - .3;
labels = cellstr([repmat('Col', ncols, 1), num2str((1:ncols)')]);
text(x, y, labels)
断爱 2024-08-28 06:12:04

您可以使用以下函数标记每个轴:

xlabel('label')
ylabel('label')

它们也可以采用单元格参数,其中每行都是一个新行。这对于显示单位很方便。可以按如下方式标记图形上的每个点:

for i=1:length(M)
    text(M(i,1),M(i,2),'Label Text')
end

标签文本也可以是一个字符串变量,您可以使用 sprintf 编辑该变量并为每个点创建特殊字符串。

You can label each axis with the function:

xlabel('label')
ylabel('label')

These can also take cell arguments, where each row is a new line. That's handy for showing units. Labeling each point on the figure can be done as so:

for i=1:length(M)
    text(M(i,1),M(i,2),'Label Text')
end

The label text can also be a string variable that you can edit with sprintf and make special strings for each point.

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