2017-08-31 4 views
0

アルゴリズムのパフォーマンスをまとめた棒グラフをプロットしたいと思います。matlabでさまざまな色とグループの棒グラフをプロットする方法

:これは3つの主なパラメータ

  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年に異なる色で、もう1つは2017で1つのfに一致しますROM2016.

なんらかの理由で、私は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

私はあなたが何をしたのか見ていますが、特定の年に同じタイプの2つのメソッドがあるときに問題がありますか? (非常に可能です) –

+0

同じ年に2つの小さなバーを2つの異なる色でプロットすることができます。 [This](https://stackoverflow.com/questions/2379230/how-to-construct-unequal-width-histograms-with-matlab) – shamalaia

+0

@itzikBenShabatに役立つかもしれませんが、私はこの新しいコードがより一般的になり、あなたの目標に近い結果を得る。 –

関連する問題