Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab绘图中的多行图例_Matlab_Legend - Fatal编程技术网

Matlab绘图中的多行图例

Matlab绘图中的多行图例,matlab,legend,Matlab,Legend,我在一个绘图上有13行,每行对应于一个文本文件中的一组数据。我想标记每一行,从第一组数据开始为1.2,然后是1.25、1.30到1.80,等等,每个增量为0.05。如果我手动输入,它将是 legend('1.20','1.25','1.30', ...., '1.80') 然而,在未来,我可能会在图表上有20多条线。所以把每一个都打出来是不现实的。我尝试在图例中创建一个循环,但没有成功 我如何以实际的方式做到这一点 这里需要帮助: legend(a+0*b,a+1*b,a+2*b, ....

我在一个绘图上有13行,每行对应于一个文本文件中的一组数据。我想标记每一行,从第一组数据开始为1.2,然后是1.25、1.30到1.80,等等,每个增量为0.05。如果我手动输入,它将是

legend('1.20','1.25','1.30', ...., '1.80')
然而,在未来,我可能会在图表上有20多条线。所以把每一个都打出来是不现实的。我尝试在图例中创建一个循环,但没有成功

我如何以实际的方式做到这一点


这里需要帮助:

legend(a+0*b,a+1*b,a+2*b, ...., a+N_FILES*b)

legend
还可以将字符串的单元格列表作为参数。试试这个:

legend_fcn = @(n)sprintf('%0.2f',a+b*n);
legend(cellfun(legend_fcn, num2cell(0:N_FILES) , 'UniformOutput', false));
我通过谷歌找到了:

图例(字符串矩阵)
添加一个包含矩阵行的图例作为标签。这与图例(字符串矩阵(1,:)、字符串矩阵(2,:)、…)相同。

所以基本上,你可以构造一个矩阵来实现这一点

例如:

strmatrix = ['a';'b';'c';'d'];

x = linspace(0,10,11);
ya = x;
yb = x+1;
yc = x+2;
yd = x+3;

figure()
plot(x,ya,x,yb,x,yc,x,yd)
legend(strmatrix)

作为构造图例的替代方法,您还可以设置行的
DisplayName
属性,以便图例自动正确

因此,您可以执行以下操作:

N_FILES = 13;
N_FRAMES = 2999;
a = 1.20; b = 0.05;

% # create colormap (look for distinguishable_colors on the File Exchange)
% # as an alternative to jet
cmap = jet(N_FILES);

x = linspace(1,N_FRAMES,N_FRAMES);

figure(1)
hold on % # make sure new plots aren't overwriting old ones

for i = 1:N_FILES
    eta = a + (i-1)*b ; 
    fname = sprintf('phi_per_timestep_eta=%3.2f.txt', eta); 
    y = load(fname);

    %# plot the line, choosing the right color and setting the displayName
    plot(x,y,'Color',cmap(i,:),'DisplayName',sprintf('%3.2f',eta));
end 

% # turn on the legend. It automatically has the right names for the curves
legend

最简单的方法可能是创建数字的列向量用作标签,使用函数将它们转换为带有
N_文件
行的格式化字符数组,然后将其作为单个参数传递给:


使用“DisplayName”作为plot()属性,并将您的图例称为

legend('-DynamicLegend');
我的代码如下所示:

x = 0:h:xmax;                                  % get an array of x-values
y = someFunction;                              % function
plot(x,y, 'DisplayName', 'Function plot 1');   % plot with 'DisplayName' property
legend('-DynamicLegend',2);                    % '-DynamicLegend' legend

来源:

为什么不直接做
x=1:N\u帧?我想更清楚。实际上你根本不需要x,
plot(phi_矩阵)应该可以工作。@yuk:那会更好,但他们必须转换
phi_矩阵
,以便它将每一列绘制为一条线。
legend('-DynamicLegend');
x = 0:h:xmax;                                  % get an array of x-values
y = someFunction;                              % function
plot(x,y, 'DisplayName', 'Function plot 1');   % plot with 'DisplayName' property
legend('-DynamicLegend',2);                    % '-DynamicLegend' legend