itzik Ben Shabat
itzik Ben Shabat

Reputation: 927

How to plot a bar graph with different colors and groups in matlab

I want to plot a bar graph which summarizes algorithms performance. It has three main parameters

  1. Publication year (the x axis)
  2. Data type (bar color)
  3. Algorithm score ( bar height)

Here is an example data:

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'};

Here I wish to get 3 bars, 2 of which in the year 2016 with different colors and one in 2017 with a color matching to one from 2016.

For some reason, I can't seem to do what I want using the bar function. It seems simple but I think I am missing something regarding how this function works. using

bar(methods_year, methods_accuracy)

gives an error :

XData cannot contain duplicate values.

Upvotes: 0

Views: 1046

Answers (1)

Calvin
Calvin

Reputation: 1359

Drawing multiple bars per category seems to be commonly done by using a matrix. Based on your comment this doesn't seem possible. It also seems that the bar() function cannot accept a vector of colors. The following code should be more general and allow you to specify the bar colors based on your 'types'.

I have modified code available at on the MATLAB Answers forum (https://ch.mathworks.com/matlabcentral/answers/310039-how-to-change-the-color-of-individual-bars-in-a-bar-chart) to account for your type of data and processing the data into the correct format.

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);

Upvotes: 0

Related Questions