Matlab 图中多条线的图例

发布于 2024-10-31 01:17:30 字数 679 浏览 2 评论 0 原文

我的绘图上有 13 条线,每条线对应于文本文件中的一组数据。我想将第一组数据开始的每一行标记为 1.2,然后是 1.25、1.30、1.80 等,每个增量为 0.05。如果我手动打出来,那就是

legend('1.20','1.25','1.30', ...., '1.80')

但是,将来我的图表上可能会有超过 20 条线。所以把每一项都打出来是不现实的。我尝试在图例中创建一个循环,但它不起作用。

我怎样才能以实际的方式做到这一点?


N_FILES=13 ; 
N_FRAMES=2999 ; 
a=1.20 ;b=0.05 ; 
phi_matrix = zeros(N_FILES,N_FRAMES) ; 
for i=1:N_FILES
    eta=a + (i-1)*b ; 
    fname=sprintf('phi_per_timestep_eta=%3.2f.txt', eta) ; 
    phi_matrix(i,:)=load(fname);
end 
figure(1);
x=linspace(1,N_FRAMES,N_FRAMES) ;
plot(x,phi_matrix) ; 

这里需要帮助:

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

I have 13 lines on a plot, each line corresponding to a set of data from a text file. I'd like to label each line starting with the first set of data as 1.2, then subsequently 1.25, 1.30, to 1.80, etc., with each increment be 0.05. If I were to type it out manually, it would be

legend('1.20','1.25','1.30', ...., '1.80')

However, in the future, I might have more than 20 lines on the graph. So typing out each one is unrealistic. I tried creating a loop in the legend and it doesn't work.

How can I do this in a practical way?


N_FILES=13 ; 
N_FRAMES=2999 ; 
a=1.20 ;b=0.05 ; 
phi_matrix = zeros(N_FILES,N_FRAMES) ; 
for i=1:N_FILES
    eta=a + (i-1)*b ; 
    fname=sprintf('phi_per_timestep_eta=%3.2f.txt', eta) ; 
    phi_matrix(i,:)=load(fname);
end 
figure(1);
x=linspace(1,N_FRAMES,N_FRAMES) ;
plot(x,phi_matrix) ; 

Need help here:

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

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

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

发布评论

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

评论(5

番薯 2024-11-07 01:17:30

作为构建图例的替代方法,您还可以设置线条的 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

As an alternative to constructing the legend, you can also set the DisplayName property of a line so that the legend is automatically correct.

Thus, you could do the following:

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
只有影子陪我不离不弃 2024-11-07 01:17:30

使用“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

来源:http ://undocumentedmatlab.com/blog/legend-semi-documented-feature/

Use 'DisplayName' as a plot() property, and call your legend as

legend('-DynamicLegend');

My code looks like this:

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

source: http://undocumentedmatlab.com/blog/legend-semi-documented-feature/

执笔绘流年 2024-11-07 01:17:30

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

legend_fcn = @(n)sprintf('%0.2f',a+b*n);
legend(cellfun(legend_fcn, num2cell(0:N_FILES) , 'UniformOutput', false));

legend can also take a cell list of strings as an argument. Try this:

legend_fcn = @(n)sprintf('%0.2f',a+b*n);
legend(cellfun(legend_fcn, num2cell(0:N_FILES) , 'UniformOutput', false));
浊酒尽余欢 2024-11-07 01:17:30

最简单的方法可能是创建一个数字的列向量用作标签,使用函数 N_FILES 行的格式化字符数组.mathworks.com/help/techdoc/ref/num2str.html" rel="nofollow">NUM2STR,然后将其作为单个参数传递给 图例

legend(num2str(a+b.*(0:N_FILES-1).','%.2f'));

The simplest approach would probably be to create a column vector of the numbers to use as your labels, convert them to a formatted character array with N_FILES rows using the function NUM2STR, then pass this as a single argument to LEGEND:

legend(num2str(a+b.*(0:N_FILES-1).','%.2f'));
池予 2024-11-07 01:17:30

我通过 Google 找到了这个 :

legend(string_matrix) 添加一个图例,其中包含矩阵 string_matrix 的行作为标签。这与 legend(string_matrix(1,:),string_matrix(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)

I found this I found through Google:

legend(string_matrix) adds a legend containing the rows of the matrix string_matrix as labels. This is the same as legend(string_matrix(1,:),string_matrix(2,:),...).

So basically, it looks like you can construct a matrix somehow to do this.

An example:

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