The legend for several lines in the Matlab graphic

I have 13 lines in the chart, each line corresponds to a data set from a text file. I would like to mark each row, starting with the first data set, as 1.2, then at 1.25, 1.30, 1.80, etc., And each step is 0.05. If I printed it manually, it would be

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

However, in the future I may have more than 20 lines on the chart. Therefore, to print each of them is unrealistic. I tried to create a loop in the legend and it does not 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) 
+4
source share
5 answers

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

So you can 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 
+7
source

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

 legend('-DynamicLegend'); 

My code is as follows:

 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/

+6
source

legend can also take a list of strings as an argument. Try the following:

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

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

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

I found this one that I found through Google:

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

Basically, it looks like you can somehow build a matrix.

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) 
0
source

Source: https://habr.com/ru/post/1347008/


All Articles