2013-07-23 252 views
1

我有使用bar和顏色映射的問題。matlab酒吧顏色表返回所有酒吧相同的顏色

我有這樣一個CSV文件,其中包含完成時間爲六個方面的工作:

34,22,103,22,171,26 
24,20,41,28,78,28 
37,19,60,23,141,24 
... 

和我創建條形圖與手段,並添加STD變化errorbar。

res = csvread('sorting_results.csv'); 
figure(); 
y = mean(res)'; 
e = std(res); 
hold on; 
bar(y); 
errorbar(y,e,'.r'); 
title('Sorting completion time'); 
ylabel('Completion time (seconds)'); 
xlabel('Task No.'); 
hold off; 
colormap(summer(size(y,2))); 

爲什麼輸出是這樣的?爲什麼酒吧有相同的顏色?我怎麼把傳說放到六個酒吧?

enter image description here

回答

0

一段代碼,它具有魔力。它不會使用@ am304提到的規範技術,因爲您很難用它設置圖例。在這裏,對於6個輸入值中的每一個,我們繪製完整的6個小節:一個小節,其餘五個設置爲零。

x = rand(1,6);  %create data 
x_diag = diag(x); %zero matrix with diagonal filled with x 
cmap = summer(6); %define colors to use (summer colomap) 

figure('color','w','Render','Zbuffer'); %create figure 

%bar plot for each x value 
for ind_data = 1:length(x) 
    h_bar = bar(x_diag(ind_data, :)); %bar plot 
    set(get(h_bar,'children'), 'FaceVertexCData', cmap(ind_data,:)) ; %color 
    hold on; 
end 
colormap('summer'); 

%legend-type info 
hleg = legend(('a':'f')'); 
set(hleg, 'box', 'off'); 
%xticks info 
set(gca, 'XTickLabel', ('a':'f')'); 

%plot errors 
e = ones(1,6) * 0.05; 
errorbar(x, e,'.r'); 
set(gca, 'FontSize', 14, 'YLim', [ 0 (max(x) + max(e) + 0.1) ]);