4

プロットには 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)
4

5 に答える 5

7

凡例を作成する代わりに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
于 2011-04-07T00:51:40.580 に答える
6

「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/

于 2013-05-27T01:22:25.520 に答える
5

legend文字列のセル リストを引数として受け取ることもできます。これを試して:

legend_fcn = @(n)sprintf('%0.2f',a+b*n);
legend(cellfun(legend_fcn, num2cell(0:N_FILES) , 'UniformOutput', false));
于 2011-04-07T00:34:48.767 に答える
1

最も簡単な方法は、ラベルとして使用する数値の列ベクトルを作成しN_FILES、関数NUM2STRを使用して行を含む書式設定された文字配列に変換し、これを単一の引数としてLEGENDに渡すことです。

legend(num2str(a+b.*(0:N_FILES-1).','%.2f'));
于 2011-04-07T17:56:21.050 に答える
0

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)
于 2011-04-07T00:35:59.837 に答える