2017-08-31 482 views
0

我想繪製一個總結算法性能的條形圖。它有三個主要參數如何在matlab中繪製具有不同顏色和組的條形圖

  1. 出版年(x軸)
  2. 數據類型(欄的顏色)
  3. 算法評分(杆高度)

下面是一個例子的數據:

dtypes = {'type1','type2','type3'}; %All of the possible data types 
methods_name = {'method1','method2','method3'}; 
methods_accuracy = [89.2, 95.54, 85]; 
methods_year = [2016, 2017, 2016] ; 
methods_dtype = {'type1', 'type2', 'type2'}; 

在這裏,我希望得到3個酒吧,其中2個在2016年用不同的顏色,一個在2017年有一個色彩匹配一個F rom 2016.

出於某種原因,我似乎無法使用bar函數來執行我想要的操作。這似乎很簡單,但我認爲我錯過了這個功能的工作原理。 使用

bar(methods_year, methods_accuracy) 

給出了一個錯誤:

XData cannot contain duplicate values. 
+0

我得到了解決,包括可能發生的碰撞,但是在組與組​​之間的杆之間距離可變的價格的任務。這可以嗎? – Gryphon

+0

我不知道我明白你的意思。年組之間沒有相同的距離是可以的。在每個年份組中,希望條間的間距相等。 –

回答

0

繪製每個類別的多條似乎是通過使用矩陣常見的做。根據你的評論,這似乎不可能。似乎bar()函數也不能接受顏色矢量。下面的代碼應該更通用一些,並允許你根據你的'類型'來指定條的顏色。

我修改了MATLAB Answers論壇(https://ch.mathworks.com/matlabcentral/answers/310039-how-to-change-the-color-of-individual-bars-in-a-bar-chart)上提供的代碼以解釋您的數據類型並將數據處理成正確的格式。

enter image description here

% code modified from https://ch.mathworks.com/matlabcentral/answers/310039-how-to-change-the-color-of-individual-bars-in-a-bar-chart 

% Plots a bar chart and gives a different color to each bar. 
% Also plots the value of the bar above the bar. 
clc; % Clear the command window. 
close all; % Close all figures (except those of imtool.) 
clear; % Erase all existing variables. 
workspace; % Make sure the workspace panel is showing. 
fontSize = 10; 
format compact 

%%%%%%%%%%%%%%%%%%%%% your data here %%%%%%%%%%%%%%%%%%%%%%%% 
dtypes = {'type1','type2','type3'}; %All of the possible data types 
methods_name = {'method1','method2','method3'}; 
methods_accuracy = [89.2, 95.54, 85]; 
methods_year = [2016, 2017, 2016] ; 
methods_dtype = {'type1', 'type2', 'type2'}; 

% sorting all vectors into values 
[sort_yrs,inds] = sort(methods_year); 
sort_methods = methods_name(inds); 
sort_dtype = methods_dtype(inds); 
sort_accuracy = methods_accuracy(inds); 
colors = zeros([length(sort_methods),3]); 
colors_types = [[1,0,0];[0,1,0];[0,0,1]]; 

for i = 1:1:length(dtypes) 
    ind1 = strfind(sort_dtype, dtypes{i}); 
    ind2 = find(not(cellfun('isempty', ind1))); 
    for j = 1:1:length(ind2) 
     colors(ind2(j),:) = colors_types(i,:); 
    end 
end 
%%%%%%%%%%% end of processing data %%%%%%%%%%%%%% 

% plotting all bars in a loop 
for b = 1 : length(sort_accuracy) 

    % plot a single bar 
    handlebar(b) = bar(b, sort_accuracy(b), 'BarWidth', 0.9); 

    % assigning color to bar 
    set(handlebar(b), 'FaceColor', colors(b,:)); 
    hold on; 
end 

% title and axis labels, if desired 
title('A title Here', 'FontSize', fontSize); 
xlabel('x', 'FontSize', fontSize); 
ylabel('y', 'FontSize', fontSize); 

% finding locations for tick labels for years, if desired 
temp = histc(sort_yrs,unique(sort_yrs)); 
% locations to use if at the middle of the year 
xTickLocs = cumsum(temp) - 0.5*temp; 
% locations to use if at the beginning of the year 
%xTickLocs = cumsum(temp) - 0.5*temp(1); 

xticks(xTickLocs); 
xticklabels(unique(sort_yrs)); 
% addind labels 
set(gca, 'XTickLabels', xticklabels); 
+0

我看到你做了什麼,但是有一個問題,當你有一個具有相同類型的兩個方法的特定年份? (這是非常可能的) –

+0

你可以在同一年用兩種不同的顏色繪製兩個小條。 [這](https://stackoverflow.com/questions/2379230/how-to-construct-unequal-width-histograms-with-matlab)可以幫助 – shamalaia

+0

@itzikBenShabat,我認爲這個新的代碼將更一般,將得到一個接近你的目標的結果。 –

相關問題