2017-05-12 11 views
0

棒グラフにスカラーTEV_IdzTEV_TFをプロットしたいとします。バーの色とデータの値が異なる:棒グラフMatlab

問題1:私は2つの異なる色で2つのスカラーの凡例を見てみたいが、私は両方の棒グラフは、同じ青色を持っている1つを得るに終わります。

問題2:それぞれのバーに各スカラーの値を取得しようとしていますが、以下の関数で出力を生成できません。

これは私の出力である:

my undesired output

これは私のコードです:

TEV_plot = bar([TEV_Idz;TEV_TF], 0.6); 
grid on; 
set(gca, 'yTickLabel',num2str(100.*get(gca,'yTick')','%g%%')); 

% PROBLEM 1: The code for having a legend 
ii = cell(1,2); 
ii{1}='L'; ii{2}='B';  %first bar called L and second bar called B 
legend(TEV_plot,ii);  %mylegend 

%PROBLEM 2: This is my code for plotting the value of each scalar on the top of the every bar graph. 
for k = 1:numel(TEV_plot) 
    text(k, TEV_plot(k), {num2str(TEV_plot(k)), ''}, ... 
     'HorizontalAlignment', 'center', ... 
     'verticalalignment', 'bottom') 
end 

答えて

1

バープロットはシリーズごとに一つの色を取ることができますので、我々はただ配置する必要があります2つのシリーズにあなたのデータではない! 各シリーズは行列の行で、行列の各列は異なる色のです。したがって、いくつかの0を追加すると(パディングは高さのある実際の棒として表示されません)、必要なものを達成できます。

これは実際に問題1を解決し、凡例を正しく動作させ、問題2を簡単にします。詳細については、

を参照してくださいコードのコメント、バーが'stacked'

TEV_Idz = 0.0018; TEV_TF = 0.012; 
% Each row in a bar plot is its own series, so put the data on two rows. 
% This can be created using TEV_data = diag([TEV_Idz, TEV_TF]); 
TEV_data = [TEV_Idz, 0; 
      0,  TEV_TF]; 
% Each column will be a different colour, plot them stacked to be central 
TEV_plot = bar(TEV_data, 0.6, 'stacked'); 
grid on; ylim([0, max(TEV_data(:) + 0.002)]); 
set(gca, 'yTickLabel',num2str(100.*get(gca,'yTick')','%g%%')); 

% Insert legend, one element for each series 
legend({'L','B'}, 'location', 'northoutside', 'orientation', 'horizontal') 
% A better option might be to just label on the x axes and not have a legend: 
% set(gca, 'xticklabels', {'L','B'}) 

% Plotting the value of each scalar on the top of the every bar graph. 
% The diagonal entries of TEV_data is where the actual data is stored. 
% Use num of diagonal entries, and index (k,k) for diagonal. 
for k = 1:numel(diag(TEV_data)) 
    text(k, TEV_data(k,k), num2str(100.*TEV_data(k,k),'%g%%'), ... 
     'HorizontalAlignment', 'center', ... 
     'verticalalignment', 'bottom') 
end 

出力でなければならない点に注意してください。

bar plot with different colour bars

関連する問題