如何使用整数向量创建图例?

问题描述 投票:0回答:1

如何使用Octave 4.4使用整数矢量创建图例?我可以使用以下代码为图创建图例:

legend({'4','8','16','32',}, 'location', 'northeast');

我不想对值列表进行硬编码,但是我想使用一个整数向量,因为它可能会根据输入数据而变化:

v=[4,8,16,32];

我尝试过,但是它创建了没有文字的图例:

legend(cellstr(num2cell(v)), 'location', 'northeast');
octave
1个回答
0
投票

摘自legend上的文档:

传奇条目可以指定为单个字符串参数,字符串或字符串的单元格数组。

使用legend可以轻松生成字符数组。您只需要注意,您的输入(数字)数组是列向量。

这是一个小例子:

num2str

这将是num2str的输出:

x = 0:100; v = [4, 8, 16, 32]; hold on; plot(x, v(1)./x); plot(x, v(2)./x); plot(x, v(3)./x); plot(x, v(4)./x); hold off; legend(num2str(v.'), 'location', 'northeast');

这将是v = [4, 8, 16, 32]的输出:

Output 1

希望有帮助!

v = [2, 3, 4, 5]
© www.soinside.com 2019 - 2024. All rights reserved.