Matlab Code – Change color of data points for each subject/group

This came about because I wanted to make a bar graph from a data set. In these data, each of my seven subjects contributed multiple data points. I wanted each bar that corresponded to a subject to be a unique color. I couldn’t find it anywhere online so here is a routine to accomplish it with either subjects as a cell array or numerical array. Note: You must have Matlab 2018a or greater. Otherwise you will get a CData error. 

%% Subjects in a cell array as strings 
data = [1 2 3 4 5 6 7 8 9]; % data should be a vector of the data you want to plot
subjects = {'A' 'B' 'C' 'A' 'B' 'C' 'A' 'B' 'C'}; % subject index should be a vector the same size as data which indicates the subject belonging to each element in your data vector
uniqueSubjects = unique(subjects);
cmap = colormap(hsv(length(uniqueSubjects))); % total subjects is the total number of subjects you have
                                     % I used the hsv colormap; use whichever one suits your needs 
b = bar(data,1,'FaceColor','flat');  % plot bar graph
    for i = 1:length(uniqueSubjects)  % for every subject
        currentSubject= uniqueSubjects{i}; % get the name of the current subject
        indexSubject = strfind(subjects,currentSubject);  % find where they are 
        index = find(not(cellfun('isempty',indexSubject )));  % get their index location
        for j = 1:length(index) % go through whole index
            b.CData(index(j),:) = cmap(i,:); % set each bar to the color corresponding to current subject
        end
    end

%% Subject names as numerical array
data = [1 2 3 4 5 6 7 8 9]; % data should be a vector of the data you want to plot
subjects = [1 2 3 1 2 3 1 2 3]; % subject index should be a vector the same size as data which indicates the subject belonging to each element in your data vector
uniqueSubjects = unique(subjects);
cmap = colormap(gray(length(uniqueSubjects ))); % total subjects is the total number of subjects you have
                                     % I used the gray colormap; use whichever one suits your needs 
b = bar(data,1,'FaceColor','flat');  % plot bar graph
    for i = 1:length(uniqueSubjects)  % for every subject
        currentSubject= uniqueSubjects(i); % get the name of the current subject
        index = find(subjects == currentSubject);  % find where they are 
        for j = 1:length(index) % go through whole index
            b.CData(index(j),:) = cmap(i,:); % set each bar to the color corresponding to current subject
        end
    end
matlab bar graph unique subject colors hsv
Cell array example
matlab bar graph unique subject colors gray
Numerical array example
Example of real data – Cyan subject tends to try for a long time before quitting. 

 

Creative Commons License
This work by Blake Porter is licensed under a Creative Commons Attribution-Non Commercial-ShareAlike 4.0 International License

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.